Skip to content

Commit e87dd3e

Browse files
committed
Add flag validation to prevent lambda execution with incorrect settings
1 parent eafae9a commit e87dd3e

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

cmd/root.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"fmt"
2121
"os"
22+
"regexp"
2223

2324
"github.com/aws/aws-lambda-go/events"
2425
"github.com/aws/aws-lambda-go/lambda"
@@ -50,6 +51,16 @@ var rootCmd = &cobra.Command{
5051
Long: `A command line tool to enable you to synchronise your Google
5152
Apps (Google Workspace) users to AWS Single Sign-on (AWS SSO)
5253
Complete documentation is available at https://github.com/awslabs/ssosync`,
54+
PreRun: func(cmd *cobra.Command, args []string) {
55+
awsGroupMatch, flagErr := cmd.Flags().GetString("aws-group-match")
56+
if flagErr != nil {
57+
log.Fatal("flag `aws-group-match` does not exist", flagErr)
58+
}
59+
_, compileErr := regexp.Compile(awsGroupMatch)
60+
if compileErr != nil {
61+
log.Fatalf("invalid aws-group-match flag value %s", awsGroupMatch, compileErr)
62+
}
63+
},
5364
RunE: func(cmd *cobra.Command, args []string) error {
5465
ctx, cancel := context.WithCancel(context.Background())
5566
defer cancel()

internal/fac/extensions.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@ import (
1010
log "github.com/sirupsen/logrus"
1111
)
1212

13-
// ErrNoAWSGroups indicates no AWS groups were received.
14-
var ErrNoAWSGroups = errors.New("received no AWS groups")
13+
// NoAWSGroupsErr indicates no AWS groups were received.
14+
var NoAWSGroupsErr = errors.New("received no AWS groups")
1515

16-
// ErrorBadRegex represents a regex compilation error.
17-
type ErrorBadRegex struct {
16+
// BadRegexError represents a regex compilation error.
17+
type BadRegexError struct {
1818
Message string
1919
Err error
2020
}
2121

22-
func (e ErrorBadRegex) Error() string {
22+
func (e BadRegexError) Error() string {
2323
return e.Message
2424
}
2525

2626
// MatchAWSGroups will filter out the AWS groups that don't match the regex.
2727
// Returns an error on failure, a list of AWS groups that match on success.
2828
func MatchAWSGroups(awsGroups []*aws.Group, matchRegex string) ([]*aws.Group, error) {
2929
if len(awsGroups) == 0 {
30-
return nil, ErrNoAWSGroups
30+
return nil, NoAWSGroupsErr
3131
}
3232

3333
awsGroupRegex, err := regexp.Compile(matchRegex)
3434
if err != nil {
35-
return nil, ErrorBadRegex{Message: fmt.Sprintf("can't compile regex %s", matchRegex), Err: err}
35+
return nil, BadRegexError{Message: fmt.Sprintf("can't compile regex %s", matchRegex), Err: err}
3636
}
3737

3838
matchedGroups := make([]*aws.Group, 0)

internal/fac/extensions_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ func TestMatchAWSGroups(t *testing.T) {
5454
name: "returns an error when input groups empty",
5555
awsGroupMatch: "aws-group-*",
5656
inputGroups: []*aws.Group{},
57-
expectedErr: ErrNoAWSGroups,
57+
expectedErr: NoAWSGroupsErr,
5858
},
5959
{
6060
name: "returns an error when input groups nil",
6161
awsGroupMatch: "aws-group-*",
6262
inputGroups: []*aws.Group{},
63-
expectedErr: ErrNoAWSGroups,
63+
expectedErr: NoAWSGroupsErr,
6464
},
6565
{
6666
name: "returns an error when regex invalid",
6767
awsGroupMatch: "[^0-1",
6868
inputGroups: []*aws.Group{{DisplayName: "aws-group-A"}},
69-
expectedErr: ErrorBadRegex{
69+
expectedErr: BadRegexError{
7070
Message: "can't compile regex [^0-1",
7171
Err: &syntax.Error{Code: syntax.ErrMissingBracket, Expr: "[^0-1"},
7272
},

internal/sync.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ func (s *syncGSuite) SyncGroupsUsers(query, awsGroupMatch string) error {
325325
onlyAWSGroupsFromGoogle, matchErr := fac.MatchAWSGroups(awsGroups, awsGroupMatch)
326326
if err != nil {
327327
log.Errorf("error filtering AWS groups by %s", matchErr)
328+
// Will continue with the full group which will delete the non Google groups.
329+
// This flow is prevented by adding pre-run flag validation.
328330
} else {
329331
awsGroups = onlyAWSGroupsFromGoogle
330332
}

0 commit comments

Comments
 (0)