Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mrWinston committed Jun 25, 2024
1 parent b2d2ab4 commit 307c148
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 156 deletions.
4 changes: 2 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func NewCmdRoot(streams genericclioptions.IOStreams) *cobra.Command {
rootCmd.AddCommand(promote.NewCmdPromote())
rootCmd.AddCommand(jira.Cmd)
rootCmd.AddCommand(cloudtrail.NewCloudtrailCmd())
rootCmd.AddCommand(managedpolicies.NewCmdManagedPolicies())
rootCmd.AddCommand(managedpolicies.NewCmdManagedPolicies())
// Add cost command to use AWS Cost Manager
rootCmd.AddCommand(cost.NewCmdCost(streams, globalOpts))

Expand All @@ -123,7 +123,7 @@ func help(cmd *cobra.Command, _ []string) {

// Checks if the version check should be run
func shouldRunVersionCheck(skipVersionCheckFlag bool, commandName string) bool {

// If either are true, then the version check should NOT run, hence negation
return !(skipVersionCheckFlag || canCommandSkipVersionCheck(commandName))
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/managedpolicies/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewCmdManagedPolicies() *cobra.Command {

managedPoliciesCommand.AddCommand(newCmdGet())
managedPoliciesCommand.AddCommand(newCmdDiff())
managedPoliciesCommand.AddCommand(newCmdSave())
managedPoliciesCommand.AddCommand(newCmdSave())

return managedPoliciesCommand
}
16 changes: 7 additions & 9 deletions cmd/managedpolicies/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ import (
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)


type diffOptions struct {
BaseVersion string
BaseVersion string
TargetVersion string
Cloud policies.CloudSpec
Cloud policies.CloudSpec
}

const (
baseVersionFlagName = "base-version"
targetVersionFlagName = "target-version"
baseVersionFlagName = "base-version"
targetVersionFlagName = "target-version"
)

func newCmdDiff() *cobra.Command {
Expand All @@ -30,7 +29,7 @@ func newCmdDiff() *cobra.Command {
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
ops.Cloud = *cmd.Flag(cloudFlagName).Value.(*policies.CloudSpec)
ops.Cloud = *cmd.Flag(cloudFlagName).Value.(*policies.CloudSpec)
cmdutil.CheckErr(ops.run())
},
}
Expand All @@ -44,13 +43,13 @@ func newCmdDiff() *cobra.Command {
}

func (o *diffOptions) run() error {
fmt.Fprintf(os.Stderr ,"Downloading Credential Requests for %s\n", o.BaseVersion)
fmt.Fprintf(os.Stderr, "Downloading Credential Requests for %s\n", o.BaseVersion)
baseDir, err := policies.DownloadCredentialRequests(o.BaseVersion, o.Cloud)
if err != nil {
return err
}

fmt.Fprintf(os.Stderr ,"Downloading Credential Requests for %s\n", o.TargetVersion)
fmt.Fprintf(os.Stderr, "Downloading Credential Requests for %s\n", o.TargetVersion)
targetDir, err := policies.DownloadCredentialRequests(o.TargetVersion, o.Cloud)
if err != nil {
return err
Expand All @@ -61,4 +60,3 @@ func (o *diffOptions) run() error {

return nil
}

4 changes: 1 addition & 3 deletions cmd/managedpolicies/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)


type getOptions struct {
ReleaseVersion string
Cloud policies.CloudSpec
Expand All @@ -21,7 +20,7 @@ func newCmdGet() *cobra.Command {
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
ops.Cloud = *cmd.Flag(cloudFlagName).Value.(*policies.CloudSpec)
ops.Cloud = *cmd.Flag(cloudFlagName).Value.(*policies.CloudSpec)
cmdutil.CheckErr(ops.run())
},
}
Expand All @@ -43,4 +42,3 @@ func (o *getOptions) run() error {

return nil
}

182 changes: 90 additions & 92 deletions cmd/managedpolicies/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,109 +14,107 @@ import (
)

type saveOptions struct {
OutFolder string
ReleaseVersion string
Cloud policies.CloudSpec
Force bool
OutFolder string
ReleaseVersion string
Cloud policies.CloudSpec
Force bool
}

func newCmdSave() *cobra.Command{
ops := &saveOptions{}

saveCmd := &cobra.Command{
Use: "save",
Short: "Save managed policies for use in mcc",
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, _ []string) {
ops.Cloud = *cmd.Flag(cloudFlagName).Value.(*policies.CloudSpec)
cmdutil.CheckErr(ops.run())
},
}
func newCmdSave() *cobra.Command {
ops := &saveOptions{}

saveCmd := &cobra.Command{
Use: "save",
Short: "Save managed policies for use in mcc",
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, _ []string) {
ops.Cloud = *cmd.Flag(cloudFlagName).Value.(*policies.CloudSpec)
cmdutil.CheckErr(ops.run())
},
}

saveCmd.Flags().StringVarP(&ops.OutFolder, "dir", "d", "", "Folder where the policy files should be written")
saveCmd.Flags().StringVarP(&ops.ReleaseVersion, "release-version", "r", "", "ocp version for which the policies should be downloaded")
saveCmd.Flags().BoolVarP(&ops.Force, "force", "f", false, "Overwrite existing files")
saveCmd.Flags().StringVarP(&ops.OutFolder, "dir", "d", "", "Folder where the policy files should be written")
saveCmd.Flags().StringVarP(&ops.ReleaseVersion, "release-version", "r", "", "ocp version for which the policies should be downloaded")
saveCmd.Flags().BoolVarP(&ops.Force, "force", "f", false, "Overwrite existing files")

saveCmd.MarkFlagRequired("out")
saveCmd.MarkFlagRequired("release-version")
saveCmd.MarkFlagRequired("out")
saveCmd.MarkFlagRequired("release-version")

return saveCmd
return saveCmd
}


func (o *saveOptions) run() error {
err := os.MkdirAll(o.OutFolder, 0755)
if err != nil {
return err
}
err := os.MkdirAll(o.OutFolder, 0755)
if err != nil {
return err
}

directory, err := policies.DownloadCredentialRequests(o.ReleaseVersion, o.Cloud)
if err != nil {
return err
}

allCredentialsRequests, err := policies.ParseCredentialsRequestsInDir(directory)
if err != nil {
return err
}

filesToCreate := map[string][]byte{}

if o.Cloud == policies.AWS {
for _, credReq := range(allCredentialsRequests) {
polDoc, err := policies.AWSCredentialsRequestToPolicyDocument(credReq)
if err != nil {
return fmt.Errorf("Error parsing CredentialsRequest '%s': %w", credReq.Name, err)
}

filename := filepath.Join(o.OutFolder, fmt.Sprintf("%s.json", credReq.Name))
out, err := json.MarshalIndent(polDoc, "", " ")
if err != nil {
return fmt.Errorf("Coulnd't Marshal sts policy '%s': %w", credReq.Name , err)
}

filesToCreate[filename] = out
}
} else if o.Cloud == policies.GCP {
for _, credReq := range(allCredentialsRequests) {
sa, err := policies.CredentialsRequestToWifServiceAccount(credReq)
if err != nil {
return fmt.Errorf("Error parsing CredentialsRequest '%s': %w", credReq.Name, err)
}

filename := filepath.Join(o.OutFolder, fmt.Sprintf("%s.yaml", sa.Id))
outJSON, err := json.Marshal(sa)
if err != nil {
return fmt.Errorf("Coulnd't Marshal wif ServiceAccount '%s': %w", sa.Id, err)
}
out, err := yaml.JSONToYAML(outJSON)
if err != nil {
return fmt.Errorf("Error Converting json to yaml: %w", err)
}
filesToCreate[filename] = out
}
}

for path, content := range(filesToCreate) {
_, err := os.Stat(path)

if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}

if err == nil && !o.Force {
fmt.Printf("Cowardly refusing to overwrite: '%s'. Append '--force' to overwrite existing files.\n", path)
continue
}

fmt.Printf("Writing %s\n", path)
if err = os.WriteFile(path, content, 0600); err != nil {
return err
}

}

return nil
}

allCredentialsRequests, err := policies.ParseCredentialsRequestsInDir(directory)
if err != nil {
return err
}

filesToCreate := map[string][]byte{}

if o.Cloud == policies.AWS {
for _, credReq := range allCredentialsRequests {
polDoc, err := policies.AWSCredentialsRequestToPolicyDocument(credReq)
if err != nil {
return fmt.Errorf("Error parsing CredentialsRequest '%s': %w", credReq.Name, err)
}

filename := filepath.Join(o.OutFolder, fmt.Sprintf("%s.json", credReq.Name))
out, err := json.MarshalIndent(polDoc, "", " ")
if err != nil {
return fmt.Errorf("Coulnd't Marshal sts policy '%s': %w", credReq.Name, err)
}

filesToCreate[filename] = out
}
} else if o.Cloud == policies.GCP {
for _, credReq := range allCredentialsRequests {
sa, err := policies.CredentialsRequestToWifServiceAccount(credReq)
if err != nil {
return fmt.Errorf("Error parsing CredentialsRequest '%s': %w", credReq.Name, err)
}

filename := filepath.Join(o.OutFolder, fmt.Sprintf("%s.yaml", sa.Id))
outJSON, err := json.Marshal(sa)
if err != nil {
return fmt.Errorf("Coulnd't Marshal wif ServiceAccount '%s': %w", sa.Id, err)
}
out, err := yaml.JSONToYAML(outJSON)
if err != nil {
return fmt.Errorf("Error Converting json to yaml: %w", err)
}
filesToCreate[filename] = out
}
}

for path, content := range filesToCreate {
_, err := os.Stat(path)

if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}

if err == nil && !o.Force {
fmt.Printf("Cowardly refusing to overwrite: '%s'. Append '--force' to overwrite existing files.\n", path)
continue
}

fmt.Printf("Writing %s\n", path)
if err = os.WriteFile(path, content, 0600); err != nil {
return err
}

}

return nil
}
2 changes: 1 addition & 1 deletion pkg/policies/cloudspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (e *CloudSpec) Set(v string) error {
case "aws", "sts":
*e = AWS
return nil
case "gcp", "wif":
case "gcp", "wif":
*e = GCP
return nil
default:
Expand Down
Loading

0 comments on commit 307c148

Please sign in to comment.