Skip to content

Commit

Permalink
Handle root user and change directory structure for go install
Browse files Browse the repository at this point in the history
  • Loading branch information
benkehoe committed Feb 21, 2023
1 parent a6c1e30 commit b8a7ddb
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ 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 }}
goarch: ${{ matrix.goarch }}
extra_files: LICENSE README.md
compress_assets: "true"
md5sum: "false"
project_path: aws-whoami
binary_name: aws-whoami
2 changes: 0 additions & 2 deletions .gitignore

This file was deleted.

4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions aws-whoami/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aws-whoami
2 changes: 1 addition & 1 deletion go.mod → aws-whoami/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/benkehoe/aws-whoami-golang
module github.com/benkehoe/aws-whoami-golang/aws-whoami

go 1.19

Expand Down
File renamed without changes.
30 changes: 19 additions & 11 deletions main.go → aws-whoami/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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 {
Expand All @@ -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})
Expand Down

0 comments on commit b8a7ddb

Please sign in to comment.