Skip to content

Commit fdf4510

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

File tree

5 files changed

+34
-39
lines changed

5 files changed

+34
-39
lines changed

.github/workflows/release.yml

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,30 @@ name: release
44
on:
55
push:
66
tags:
7-
- '*'
7+
- 'v[0-9]+.[0-9]+.[0-9]+*'
88

9-
jobs:
10-
test:
11-
runs-on: ubuntu-latest
12-
steps:
13-
- name: Check out code into the Go module directory
14-
uses: actions/checkout@v3
15-
16-
- name: Setup go
17-
uses: actions/setup-go@v4
18-
with:
19-
go-version: '1.20.x'
20-
21-
- name: Install staticcheck
22-
run: go install honnef.co/go/tools/cmd/staticcheck@latest
23-
24-
- name: Run staticcheck
25-
run: staticcheck ./...
26-
27-
- name: Run Tests
28-
run: go test -p 1 -cover -race -v ./...
9+
permissions:
10+
contents: write
2911

12+
jobs:
3013
release:
3114
runs-on: ubuntu-latest
32-
needs: [ test ]
3315
steps:
3416
- name: Checkout
35-
uses: actions/checkout@v3
36-
37-
- name: Unshallow
38-
run: git fetch --prune --unshallow
17+
uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0
20+
fetch-tags: true
3921

4022
- name: Set up Go
4123
uses: actions/setup-go@v4
4224
with:
43-
go-version: '1.20.x'
25+
go-version: '1.21.x'
4426

4527
- name: Run GoReleaser
46-
uses: goreleaser/goreleaser-action@v4
28+
uses: goreleaser/goreleaser-action@v5
4729
with:
4830
version: latest
49-
args: release --rm-dist
31+
args: release --clean
5032
env:
5133
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

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)