Skip to content
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

Feature: #107 Addition of a dry run flag #218

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 104 additions & 103 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/codepipeline"
"github.com/aws/aws-sdk-go/service/codepipeline"
"github.com/aws/aws-sdk-go/service/secretsmanager"
"github.com/awslabs/ssosync/internal"
"github.com/awslabs/ssosync/internal/config"
Expand Down Expand Up @@ -67,72 +67,72 @@ Complete documentation is available at https://github.com/awslabs/ssosync`,
// running inside of AWS Lambda, we use the Lambda
// execution path.
func Execute() {
if cfg.IsLambda {
log.Info("Executing as Lambda")
lambda.Start(Handler)
}

if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
if cfg.IsLambda {
log.Info("Executing as Lambda")
lambda.Start(Handler)
}

if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}

// Handler for when executing as a lambda
func Handler(ctx context.Context, event events.CodePipelineEvent) (string, error) {
log.Debug(event)
err := rootCmd.Execute()
s := session.Must(session.NewSession())
cpl := codepipeline.New(s)

cfg.IsLambdaRunningInCodePipeline = len(event.CodePipelineJob.ID) > 0

if cfg.IsLambdaRunningInCodePipeline {
log.Info("Lambda has been invoked by CodePipeline")

if err != nil {
// notify codepipeline and mark its job execution as Failure
log.Fatalf(errors.Wrap(err, "Notifying CodePipeline and mark its job execution as Failure").Error())
jobID := event.CodePipelineJob.ID
if len(jobID) == 0 {
panic("CodePipeline Job ID is not set")
}
// mark the job as Failure.
cplFailure := &codepipeline.PutJobFailureResultInput{
JobId: aws.String(jobID),
FailureDetails: &codepipeline.FailureDetails{
Message: aws.String(err.Error()),
Type: aws.String("JobFailed"),
},
}
_, cplErr := cpl.PutJobFailureResult(cplFailure)
if cplErr != nil {
log.Fatalf(errors.Wrap(err, "Failed to update CodePipeline jobID status").Error())
}
return "Failure", err
}

log.Info("Notifying CodePipeline and mark its job execution as Success")
jobID := event.CodePipelineJob.ID
if len(jobID) == 0 {
panic("CodePipeline Job ID is not set")
}
// mark the job as Success.
cplSuccess := &codepipeline.PutJobSuccessResultInput{
JobId: aws.String(jobID),
}
_, cplErr := cpl.PutJobSuccessResult(cplSuccess)
if cplErr != nil {
log.Fatalf(errors.Wrap(err, "Failed to update CodePipeline jobID status").Error())
}

log.Debug(event)
err := rootCmd.Execute()
s := session.Must(session.NewSession())
cpl := codepipeline.New(s)

cfg.IsLambdaRunningInCodePipeline = len(event.CodePipelineJob.ID) > 0

if cfg.IsLambdaRunningInCodePipeline {
log.Info("Lambda has been invoked by CodePipeline")

if err != nil {
// notify codepipeline and mark its job execution as Failure
log.Fatalf(errors.Wrap(err, "Notifying CodePipeline and mark its job execution as Failure").Error())
jobID := event.CodePipelineJob.ID
if len(jobID) == 0 {
panic("CodePipeline Job ID is not set")
}
// mark the job as Failure.
cplFailure := &codepipeline.PutJobFailureResultInput{
JobId: aws.String(jobID),
FailureDetails: &codepipeline.FailureDetails{
Message: aws.String(err.Error()),
Type: aws.String("JobFailed"),
},
}
_, cplErr := cpl.PutJobFailureResult(cplFailure)
if cplErr != nil {
log.Fatalf(errors.Wrap(err, "Failed to update CodePipeline jobID status").Error())
}
return "Failure", err
}

log.Info("Notifying CodePipeline and mark its job execution as Success")
jobID := event.CodePipelineJob.ID
if len(jobID) == 0 {
panic("CodePipeline Job ID is not set")
}
// mark the job as Success.
cplSuccess := &codepipeline.PutJobSuccessResultInput{
JobId: aws.String(jobID),
}
_, cplErr := cpl.PutJobSuccessResult(cplSuccess)
if cplErr != nil {
log.Fatalf(errors.Wrap(err, "Failed to update CodePipeline jobID status").Error())
}

return "Success", nil
}

if err != nil {
log.Fatalf(errors.Wrap(err, "Notifying Lambda and mark this execution as Failure").Error())
return "Failure", err
}
return "Success", nil
}

if err != nil {
log.Fatalf(errors.Wrap(err, "Notifying Lambda and mark this execution as Failure").Error())
return "Failure", err
}
return "Success", nil
}

func init() {
Expand Down Expand Up @@ -194,7 +194,7 @@ func initConfig() {
}

func configLambda() {
s := session.Must(session.NewSession())
s := session.Must(session.NewSession())
svc := secretsmanager.New(s)
secrets := config.NewSecrets(svc)

Expand Down Expand Up @@ -234,53 +234,53 @@ func configLambda() {
}
cfg.IdentityStoreID = unwrap

unwrap = os.Getenv("LOG_LEVEL")
if len([]rune(unwrap)) != 0 {
cfg.LogLevel = unwrap
log.WithField("LogLevel", unwrap).Debug("from EnvVar")
}
unwrap = os.Getenv("LOG_LEVEL")
if len([]rune(unwrap)) != 0 {
cfg.LogLevel = unwrap
log.WithField("LogLevel", unwrap).Debug("from EnvVar")
}

unwrap = os.Getenv("LOG_FORMAT")
if len([]rune(unwrap)) != 0 {
cfg.LogFormat = unwrap
log.WithField("LogFormay", unwrap).Debug("from EnvVar")
}
unwrap = os.Getenv("LOG_FORMAT")
if len([]rune(unwrap)) != 0 {
cfg.LogFormat = unwrap
log.WithField("LogFormay", unwrap).Debug("from EnvVar")
}

unwrap = os.Getenv("SYNC_METHOD")
if len([]rune(unwrap)) != 0 {
cfg.SyncMethod = unwrap
log.WithField("SyncMethod", unwrap).Debug("from EnvVar")
}
if len([]rune(unwrap)) != 0 {
cfg.SyncMethod = unwrap
log.WithField("SyncMethod", unwrap).Debug("from EnvVar")
}

unwrap = os.Getenv("USER_MATCH")
if len([]rune(unwrap)) != 0 {
cfg.UserMatch = unwrap
log.WithField("UserMatch", unwrap).Debug("from EnvVar")
}
if len([]rune(unwrap)) != 0 {
cfg.UserMatch = unwrap
log.WithField("UserMatch", unwrap).Debug("from EnvVar")
}

unwrap = os.Getenv("GROUP_MATCH")
if len([]rune(unwrap)) != 0 {
cfg.GroupMatch = unwrap
log.WithField("GroupMatch", unwrap).Debug("from EnvVar")
}

unwrap = os.Getenv("IGNORE_GROUPS")
if len([]rune(unwrap)) != 0 {
cfg.IgnoreGroups = strings.Split(unwrap, ",")
log.WithField("IgnoreGroups", unwrap).Debug("from EnvVar")
}

unwrap = os.Getenv("IGNORE_USERS")
if len([]rune(unwrap)) != 0 {
cfg.IgnoreUsers = strings.Split(unwrap, ",")
log.WithField("IgnoreUsers", unwrap).Debug("from EnvVar")
}

unwrap = os.Getenv("INCLUDE_GROUPS")
if len([]rune(unwrap)) != 0 {
cfg.IncludeGroups = strings.Split(unwrap, ",")
log.WithField("IncludeGroups", unwrap).Debug("from EnvVar")
}
if len([]rune(unwrap)) != 0 {
cfg.GroupMatch = unwrap
log.WithField("GroupMatch", unwrap).Debug("from EnvVar")
}

unwrap = os.Getenv("IGNORE_GROUPS")
if len([]rune(unwrap)) != 0 {
cfg.IgnoreGroups = strings.Split(unwrap, ",")
log.WithField("IgnoreGroups", unwrap).Debug("from EnvVar")
}

unwrap = os.Getenv("IGNORE_USERS")
if len([]rune(unwrap)) != 0 {
cfg.IgnoreUsers = strings.Split(unwrap, ",")
log.WithField("IgnoreUsers", unwrap).Debug("from EnvVar")
}

unwrap = os.Getenv("INCLUDE_GROUPS")
if len([]rune(unwrap)) != 0 {
cfg.IncludeGroups = strings.Split(unwrap, ",")
log.WithField("IncludeGroups", unwrap).Debug("from EnvVar")
}

}

Expand All @@ -301,6 +301,7 @@ func addFlags(cmd *cobra.Command, cfg *config.Config) {
rootCmd.Flags().StringVarP(&cfg.SyncMethod, "sync-method", "s", config.DefaultSyncMethod, "Sync method to use (users_groups|groups)")
rootCmd.Flags().StringVarP(&cfg.Region, "region", "r", "", "AWS Region where AWS SSO is enabled")
rootCmd.Flags().StringVarP(&cfg.IdentityStoreID, "identity-store-id", "i", "", "Identifier of Identity Store in AWS SSO")
rootCmd.PersistentFlags().BoolVarP(&cfg.DryRun, "dry-run", "n", config.DefaultDebug, "Print the commands, but do not perform any modifications.")
}

func logConfig(cfg *config.Config) {
Expand Down
4 changes: 3 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Config struct {
SCIMAccessToken string `mapstructure:"scim_access_token"`
// IsLambda ...
IsLambda bool
// IsLambdaRunningInCodePipeline ...
// IsLambdaRunningInCodePipeline ...
IsLambdaRunningInCodePipeline bool
// Ignore users ...
IgnoreUsers []string `mapstructure:"ignore_users"`
Expand All @@ -37,6 +37,8 @@ type Config struct {
Region string `mapstructure:"region"`
// IdentityStoreID is the ID of the identity store
IdentityStoreID string `mapstructure:"identity_store_id"`
// Print the commands, but do not perform any modifications.
DryRun bool
}

const (
Expand Down
Loading