From b8a7ddb80df6c8e1354402d048d6a44219763d52 Mon Sep 17 00:00:00 2001 From: Ben Kehoe Date: Tue, 21 Feb 2023 13:59:21 +0000 Subject: [PATCH] Handle root user and change directory structure for go install --- .github/workflows/release.yaml | 3 ++- .gitignore | 2 -- CHANGELOG.md | 4 ++++ README.md | 6 +++++- aws-whoami/.gitignore | 1 + go.mod => aws-whoami/go.mod | 2 +- go.sum => aws-whoami/go.sum | 0 main.go => aws-whoami/main.go | 30 +++++++++++++++++++----------- 8 files changed, 32 insertions(+), 16 deletions(-) delete mode 100644 .gitignore create mode 100644 aws-whoami/.gitignore rename go.mod => aws-whoami/go.mod (93%) rename go.sum => aws-whoami/go.sum (100%) rename main.go => aws-whoami/main.go (89%) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 1417e66..72d4f88 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -16,7 +16,7 @@ jobs: goos: windows steps: - uses: actions/checkout@v3 - - uses: wangyoucao577/go-release-action@v1.30 + - uses: wangyoucao577/go-release-action@v1.35 with: github_token: ${{ secrets.GITHUB_TOKEN }} goos: ${{ matrix.goos }} @@ -24,4 +24,5 @@ jobs: extra_files: LICENSE README.md compress_assets: "true" md5sum: "false" + project_path: aws-whoami binary_name: aws-whoami diff --git a/.gitignore b/.gitignore deleted file mode 100644 index c486b6e..0000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -aws-whoami -aws-whoami-golang diff --git a/CHANGELOG.md b/CHANGELOG.md index cc1f033..735b694 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ `aws-whoami` uses [monotonic versioning](https://github.com/benkehoe/monotonic-versioning-manifesto) since v1.0 across both [the older Python implementation](https://github.com/benkehoe/aws-whoami) (compatibility number 1) and this Go implementation (compatibility number 2). +## v2.4 + +* Handle root user + ## v2.3 * Initial implementation in Go. diff --git a/README.md b/README.md index 285ea6e..62d32b4 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,11 @@ your account alias won't appear. See below for disabling this check if getting a ## Install -[Download the latest release for your platform](https://github.com/benkehoe/aws-whoami-golang/releases). +``` +go install github.com/benkehoe/aws-whoami-golang/aws-whoami@latest +``` + +[Or download the latest release for your platform](https://github.com/benkehoe/aws-whoami-golang/releases/latest). ## Options diff --git a/aws-whoami/.gitignore b/aws-whoami/.gitignore new file mode 100644 index 0000000..ebe4dc0 --- /dev/null +++ b/aws-whoami/.gitignore @@ -0,0 +1 @@ +aws-whoami diff --git a/go.mod b/aws-whoami/go.mod similarity index 93% rename from go.mod rename to aws-whoami/go.mod index 4044bd5..80cee29 100644 --- a/go.mod +++ b/aws-whoami/go.mod @@ -1,4 +1,4 @@ -module github.com/benkehoe/aws-whoami-golang +module github.com/benkehoe/aws-whoami-golang/aws-whoami go 1.19 diff --git a/go.sum b/aws-whoami/go.sum similarity index 100% rename from go.sum rename to aws-whoami/go.sum diff --git a/main.go b/aws-whoami/main.go similarity index 89% rename from main.go rename to aws-whoami/main.go index 7f6f78f..f8821d5 100644 --- a/main.go +++ b/aws-whoami/main.go @@ -32,7 +32,7 @@ import ( "github.com/aws/smithy-go" ) -var Version string = "2.3" +var Version string = "2.4" type Whoami struct { Account string @@ -125,10 +125,15 @@ func NewWhoami(awsConfig aws.Config, params WhoamiParams) (Whoami, error) { whoami.UserId = *getCallerIdentityOutput.UserId arnFields := strings.Split(whoami.Arn, ":") - arnResourceFields := strings.SplitN(arnFields[len(arnFields)-1], "/", 2) - if len(arnResourceFields) < 2 { - return whoami, fmt.Errorf("arn %v has an unknown format", whoami.Arn) + var arnResourceFields []string + if arnFields[len(arnFields)-1] == "root" { + arnResourceFields = []string{"root", "root"} + } else { + arnResourceFields = strings.SplitN(arnFields[len(arnFields)-1], "/", 2) + if len(arnResourceFields) < 2 { + return whoami, fmt.Errorf("arn %v has an unknown format", whoami.Arn) + } } whoami.Type = arnResourceFields[0] @@ -179,15 +184,18 @@ type record struct { value string } -func getTypeField(typeName string) string { - fields := strings.Split(typeName, "-") - values := make([]string, 0, 3) +func getTypeNameRecord(whoami Whoami) record { + if whoami.Type == "root" { + return record{"Type: ", "root"} + } + fields := strings.Split(whoami.Type, "-") + typeParts := make([]string, 0, 3) for _, field := range fields { s := strings.ToUpper(field[:1]) + field[1:] // ok because always ASCII - values = append(values, s) + typeParts = append(typeParts, s) } - values = append(values, ": ") - return strings.Join(values, "") + typeParts = append(typeParts, ": ") + return record{strings.Join(typeParts, ""), whoami.Name} } func (whoami Whoami) Format() string { @@ -200,7 +208,7 @@ func (whoami Whoami) Format() string { if whoami.SSOPermissionSet != nil { records = append(records, record{"AWS SSO: ", *whoami.SSOPermissionSet}) } else { - records = append(records, record{getTypeField(whoami.Type), whoami.Name}) + records = append(records, getTypeNameRecord(whoami)) } if whoami.RoleSessionName != nil { records = append(records, record{"RoleSessionName: ", *whoami.RoleSessionName})