-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a show command to the Diki CLI #412
Add a show command to the Diki CLI #412
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I did an initial review. I would probably go once again over the PR once the comments are addressed.
cmd/diki/app/app.go
Outdated
Short: "", | ||
Long: "", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Short: "", | |
Long: "", | |
Short: "Show detailed information for the given provider.", | |
Long: "Show detailed information for the given provider.", |
pkg/metadata/metadata.go
Outdated
@@ -0,0 +1,41 @@ | |||
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Gardener contributors |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Gardener contributors | |
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors |
pkg/metadata/metadata.go
Outdated
|
||
// Version is used to represent a specific version of a ruleset | ||
type Version struct { | ||
// Version is the human-readable name of the ruleset release |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Version is the human-readable name of the ruleset release | |
// Version is the name of the ruleset release. |
pkg/metadata/metadata.go
Outdated
type Version struct { | ||
// Version is the human-readable name of the ruleset release | ||
Version string `json:"version"` | ||
// Latest is a bool tag that showcases if the specific version is the latest one |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Latest is a bool tag that showcases if the specific version is the latest one | |
// Latest shows if the specific version is the latest one. |
pkg/metadata/metadata.go
Outdated
ProviderName string `json:"name"` | ||
} | ||
|
||
// ProviderMetadata is used to represent a specific provider and it's metadata |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put dots at the end of all sentences.
pkg/provider/builder/garden.go
Outdated
// GardenProviderMetadata returns available metadata for the Garden Provider and it's supported rulesets. | ||
func GardenProviderMetadata() metadata.ProviderMetadata { | ||
providerMetadata := metadata.ProviderMetadata{} | ||
providerMetadata.ProviderID = "garden" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would not be correct if the provider id changes here
Lines 19 to 22 in a4a8e8f
"garden": builder.GardenProviderFromConfig, | |
"gardener": builder.GardenerProviderFromConfig, | |
"managedk8s": builder.ManagedK8SProviderFromConfig, | |
"virtualgarden": builder.VirtualGardenProviderFromConfig, |
We should either fix the provider id to a constant or not hardcode it here.
pkg/provider/builder/garden.go
Outdated
var availableRulesets = map[string]string{ | ||
securityhardenedshoot.RulesetID: securityhardenedshoot.RulesetName, | ||
} | ||
|
||
for rulesetID, rulesetName := range availableRulesets { | ||
rulesetMetadata := &metadata.RulesetMetadata{} | ||
rulesetMetadata.RulesetID = rulesetID | ||
rulesetMetadata.RulesetName = rulesetName | ||
rulesetSupportedVersions := gardenGetSupportedVersions(rulesetMetadata.RulesetID) | ||
|
||
for index, supportedVersion := range rulesetSupportedVersions { | ||
if index == 0 { | ||
rulesetMetadata.Versions = append(rulesetMetadata.Versions, metadata.Version{Version: supportedVersion, Latest: true}) | ||
} else { | ||
rulesetMetadata.Versions = append(rulesetMetadata.Versions, metadata.Version{Version: supportedVersion, Latest: false}) | ||
} | ||
} | ||
providerMetadata.ProviderRulesets = append(providerMetadata.ProviderRulesets, *rulesetMetadata) | ||
} | ||
|
||
return providerMetadata |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just initialize the structs?
providerMetadata := metadata.ProviderMetadata{
ProviderID: "garden",
ProviderName: "Garden",
ProviderRulesets: []metadata.RulesetMetadata{
{
RulesetID: securityhardenedshoot.RulesetID,
RulesetName: securityhardenedshoot.RulesetName,
},
},
}
for i := range providerMetadata.ProviderRulesets {
supportedVersions := gardenGetSupportedVersions(providerMetadata.ProviderRulesets[i].RulesetID)
for _, supportedVersion := range supportedVersions {
providerMetadata.ProviderRulesets[i].Versions = append(
providerMetadata.ProviderRulesets[i].Versions,
metadata.Version{Version: supportedVersion, Latest: false},
)
}
// Mark the first version as latest as the versions are sorted from newest to oldest
if len(providerMetadata.ProviderRulesets[i].Versions) > 0 {
providerMetadata.ProviderRulesets[i].Versions[0].Latest = true
}
}
return providerMetadata
cmd/diki/app/app.go
Outdated
} else { | ||
var providerArg = args[0] | ||
|
||
metadataFunc, ok := providerFuncMap[providerArg] | ||
if !ok { | ||
return fmt.Errorf("provider %s does not exist in the current diki binary", providerArg) | ||
} | ||
|
||
providerMetadata := metadataFunc() | ||
|
||
if bytes, err := json.Marshal(providerMetadata); err != nil { | ||
return err | ||
} else { | ||
fmt.Println(string(bytes)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} else { | |
var providerArg = args[0] | |
metadataFunc, ok := providerFuncMap[providerArg] | |
if !ok { | |
return fmt.Errorf("provider %s does not exist in the current diki binary", providerArg) | |
} | |
providerMetadata := metadataFunc() | |
if bytes, err := json.Marshal(providerMetadata); err != nil { | |
return err | |
} else { | |
fmt.Println(string(bytes)) | |
} | |
} | |
} | |
metadataFunc, ok := providerFuncMap[args[0]] | |
if !ok { | |
return fmt.Errorf("provider %s does not exist in the current diki binary", args[0]) | |
} | |
if bytes, err := json.Marshal(metadataFunc()); err != nil { | |
return err | |
} | |
fmt.Println(string(bytes)) | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few minor remarks
cmd/diki/app/app.go
Outdated
@@ -124,6 +125,28 @@ e.g. to check compliance of your hyperscaler accounts.`, | |||
addReportGenerateDiffFlags(generateDiffCmd, &generateDiffOpts) | |||
generateCmd.AddCommand(generateDiffCmd) | |||
|
|||
showCmd := &cobra.Command{ | |||
Use: "show", | |||
Short: "Show metadata of the providers that the current diki binary supports.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Short: "Show metadata of the providers that the current diki binary supports.", | |
Short: "Show metadata information for different diki internals, i.e. providers.", |
cmd/diki/app/app.go
Outdated
|
||
showProviderCmd := &cobra.Command{ | ||
Use: "provider", | ||
Short: "Show detailed information for the given provider.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Short: "Show detailed information for the given provider.", | |
Short: "Show detailed information for providers.", |
cmd/diki/app/app.go
Outdated
|
||
metadataFunc, ok := metadataFuncs[args[0]] | ||
if !ok { | ||
return fmt.Errorf("provider %s does not exist in the current diki binary", args[0]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return fmt.Errorf("provider %s does not exist in the current diki binary", args[0]) | |
return fmt.Errorf("unknown provider: %s", args[0]) |
cmd/diki/app/app.go
Outdated
"github.com/gardener/diki/pkg/provider" | ||
"github.com/gardener/diki/pkg/report" | ||
"github.com/gardener/diki/pkg/ruleset" | ||
) | ||
|
||
// NewDikiCommand creates a new command that is used to start Diki. | ||
func NewDikiCommand(providerCreateFuncs map[string]provider.ProviderFromConfigFunc) *cobra.Command { | ||
func NewDikiCommand(providerCreateFuncs map[string]provider.ProviderFromConfigFunc, metadataFuncs map[string]metadata.MetadataFunc) *cobra.Command { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a single map, so there is consistency across provider id keys.
cmd/diki/app/app.go
Outdated
@@ -156,6 +179,39 @@ func addReportGenerateDiffFlags(cmd *cobra.Command, opts *generateDiffOptions) { | |||
cmd.PersistentFlags().Var(cliflag.NewMapStringString(&opts.identityAttributes), "identity-attributes", "The keys are the IDs of the providers that will be present in the generated difference report and the values are metadata attributes to be used as identifiers.") | |||
} | |||
|
|||
func showProviderCmd(args []string, metadataFuncs map[string]metadata.MetadataFunc) error { | |||
if len(args) > 1 { | |||
return errors.New("command `show provider` accepts at most one provider") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return errors.New("command `show provider` accepts at most one provider") | |
return errors.New("command 'show provider' accepts at most one provider") |
pkg/metadata/metadata.go
Outdated
} | ||
|
||
// MetadataFunc constructs a detailed Provider metadata object. | ||
type MetadataFunc func() ProviderDetailed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this func to pkg/provider/provider.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
What this PR does / why we need it:
This PR implements a new Cobra command that generates a JSON object containing the metadata of the specified provider and it's supported rulesets, or a JSON list of all providers available.
Which issue(s) this PR fixes:
Fixes #300
Special notes for your reviewer:
Release note: