Skip to content

Commit 616f8d9

Browse files
authored
feat: Show cloud identity being currently used (#675)
* show azure account * show signed-in user * show aws caller identity * show aws profile
1 parent 99f8d41 commit 616f8d9

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

pkg/provider/aws.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package provider
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"os/exec"
78
"sort"
89
"strings"
@@ -72,11 +73,16 @@ var (
7273
func mkAWS(conf config.Config) (provider *AWSProvider, err error) {
7374
ctx := context.Background()
7475

75-
iamSession, err := GetAWSCallerIdentity(ctx)
76+
iamSession, callerIdentity, err := GetAWSCallerIdentity(ctx)
7677
if err != nil {
7778
return nil, plrlErrors.ErrorWrap(err, "Failed to get AWS caller identity")
7879
}
7980

81+
fmt.Printf("\nUsing %s AWS profile\n", getAWSProfileName())
82+
fmt.Printf("Caller identity ARN: %s\n", lo.FromPtr(callerIdentity.Arn))
83+
fmt.Printf("Caller identity account: %s\n", lo.FromPtr(callerIdentity.Account))
84+
fmt.Printf("Caller identity user ID: %s\n\n", lo.FromPtr(callerIdentity.UserId))
85+
8086
provider = &AWSProvider{
8187
goContext: &ctx,
8288
ctx: map[string]any{
@@ -402,29 +408,41 @@ func (aws *AWSProvider) testIamPermissions() error {
402408
return fmt.Errorf("you do not meet all required iam permissions to deploy an eks cluster: %s, this is not necessarily a full list, we recommend using as close to AdministratorAccess as possible to run plural", strings.Join(missing, ","))
403409
}
404410

411+
func getAWSProfileName() string {
412+
if profile := os.Getenv("AWS_PROFILE"); profile != "" {
413+
return profile
414+
}
415+
416+
if profile := os.Getenv("AWS_DEFAULT_PROFILE"); profile != "" {
417+
return profile
418+
}
419+
420+
return "default"
421+
}
422+
405423
// GetAWSCallerIdentity returns the IAM role ARN of the current caller identity.
406-
func GetAWSCallerIdentity(ctx context.Context) (string, error) {
424+
func GetAWSCallerIdentity(ctx context.Context) (string, *sts.GetCallerIdentityOutput, error) {
407425
cfg, err := getAwsConfig(ctx)
408426
if err != nil {
409-
return "", err
427+
return "", nil, err
410428
}
411429

412430
svc := sts.NewFromConfig(cfg)
413431
callerIdentity, err := svc.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{})
414432
if err != nil {
415-
return "", plrlErrors.ErrorWrap(err, "Error getting caller identity: ")
433+
return "", callerIdentity, plrlErrors.ErrorWrap(err, "Error getting caller identity: ")
416434
}
417435

418436
callerIdentityArn := lo.FromPtr(callerIdentity.Arn)
419437
roleName, _ := RoleNameSessionFromARN(callerIdentityArn)
420438
if !lo.IsEmpty(roleName) {
421439
role, err := iam.NewFromConfig(cfg).GetRole(ctx, &iam.GetRoleInput{RoleName: &roleName})
422440
if err != nil {
423-
return "", plrlErrors.ErrorWrap(err, "Error getting IAM role: ")
441+
return "", callerIdentity, plrlErrors.ErrorWrap(err, "Error getting IAM role: ")
424442
}
425443

426-
return lo.FromPtr(role.Role.Arn), nil
444+
return lo.FromPtr(role.Role.Arn), callerIdentity, nil
427445
}
428446

429-
return callerIdentityArn, nil
447+
return callerIdentityArn, callerIdentity, nil
430448
}

pkg/provider/azure.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,20 @@ type AzureProvider struct {
115115
}
116116

117117
func mkAzure(conf config.Config) (prov *AzureProvider, err error) {
118-
subId, tenID, err := GetAzureAccount()
118+
subId, tenID, subName, err := GetAzureAccount()
119119
if err != nil {
120120
return
121121
}
122122

123+
user, err := GetAzureUser()
124+
if err != nil {
125+
return
126+
}
127+
128+
fmt.Printf("\nLogged in as %s to %s Azure subscription\n", user, subName)
129+
fmt.Printf("Subscription ID: %s\n", subId)
130+
fmt.Printf("Tenant ID: %s\n\n", tenID)
131+
123132
clients, err := GetClientSet(subId)
124133
if err != nil {
125134
return
@@ -455,23 +464,40 @@ func (az *AzureProvider) upsertStorageContainer(acc armstorage.Account, name str
455464
return err
456465
}
457466

458-
func GetAzureAccount() (string, string, error) {
467+
func GetAzureAccount() (string, string, string, error) {
459468
cmd := exec.Command("az", "account", "show")
460469
out, err := cmd.Output()
461470
if err != nil {
462471
fmt.Println(string(out))
463-
return "", "", err
472+
return "", "", "", err
464473
}
465474

466475
var res struct {
467476
TenantId string
468477
Id string
478+
Name string
469479
}
470480

471481
if err := json.Unmarshal(out, &res); err != nil {
472-
return "", "", err
482+
return "", "", "", err
473483
}
474-
return res.Id, res.TenantId, nil
484+
return res.Id, res.TenantId, res.Name, nil
485+
}
486+
487+
func GetAzureUser() (string, error) {
488+
cmd := exec.Command("az", "ad", "signed-in-user", "show")
489+
out, err := cmd.Output()
490+
if err != nil {
491+
fmt.Println(string(out))
492+
return "", err
493+
}
494+
495+
var res struct{ UserPrincipalName string }
496+
if err := json.Unmarshal(out, &res); err != nil {
497+
return "", err
498+
}
499+
500+
return res.UserPrincipalName, nil
475501
}
476502

477503
func isNotFoundResourceGroup(err error) bool {
@@ -507,7 +533,7 @@ func getPathElement(path, indexName string) (string, error) {
507533
}
508534

509535
func ValidateAzureDomainRegistration(ctx context.Context, domain, resourceGroup string) error {
510-
subId, _, err := GetAzureAccount()
536+
subId, _, _, err := GetAzureAccount()
511537
if err != nil {
512538
return err
513539
}

0 commit comments

Comments
 (0)