Updating assume role functionality

This commit is contained in:
2026-04-03 00:42:22 -04:00
parent a70c88342b
commit 73d1e7967d
2 changed files with 19 additions and 19 deletions

View File

@@ -44,8 +44,6 @@ func authLocalAuthenticate(args []string) {
authCmd.StringVar(&authAccountid, "account", "", "Account ID (required)") authCmd.StringVar(&authAccountid, "account", "", "Account ID (required)")
authCmd.StringVar(&outputFormat, "o", "json", "Output format (text or json)") authCmd.StringVar(&outputFormat, "o", "json", "Output format (text or json)")
outputFormat = "json"
authCmd.Parse(args) authCmd.Parse(args)
if authAccountid == "" { if authAccountid == "" {

36
role.go
View File

@@ -39,19 +39,22 @@ var roleCommands = []Command{
} }
func assumeRole(args []string) { func assumeRole(args []string) {
fmt.Println("I assume something")
var targetRole string var targetRole string
var useIdentity string var useIdentity string
var outputFormat string
roleAssumeRoleCmd := flag.NewFlagSet("assume-role", flag.ExitOnError) roleAssumeRoleCmd := flag.NewFlagSet("assume-role", flag.ExitOnError)
roleAssumeRoleCmd.StringVar(&targetRole, "r", "", "Target role (required)") roleAssumeRoleCmd.StringVar(&targetRole, "r", "", "Target role (required)")
roleAssumeRoleCmd.StringVar(&targetRole, "role", "", "Target role (required)") roleAssumeRoleCmd.StringVar(&targetRole, "rolename", "", "Target role (required)")
roleAssumeRoleCmd.StringVar(&useIdentity, "identity", "", "Identity to use (required)") roleAssumeRoleCmd.StringVar(&useIdentity, "identity", "", "Identity to use (required)")
roleAssumeRoleCmd.StringVar(&outputFormat, "output", "json", "Output format (json or text)")
roleAssumeRoleCmd.Parse(args) roleAssumeRoleCmd.Parse(args)
fmt.Println("output format is", outputFormat)
if targetRole == "" { if targetRole == "" {
fmt.Println("Error: either -r or --role is required") fmt.Println("Error: either -r or --rolename is required")
os.Exit(1) os.Exit(1)
} }
if useIdentity == "" { if useIdentity == "" {
@@ -106,16 +109,6 @@ func assumeRole(args []string) {
os.Exit(1) os.Exit(1)
} }
// fmt.Println("Response status:", resp.Status)
fmt.Println("Assumed role token:", apiresponse.Content.Token)
// fmt.Printf("Attaching policy '%s' to role '%s'\n", , targetRole)
fmt.Println("Response status:", resp.Status)
fmt.Printf("Assuming role '%s'\n", targetRole)
fmt.Println("Using identity", resp.Body)
// Write apiresponse.Content to ~/.pcloud/roles/assumed-<rolename>.json
home, err := os.UserHomeDir() home, err := os.UserHomeDir()
if err != nil { if err != nil {
panic(err) panic(err)
@@ -134,6 +127,15 @@ func assumeRole(args []string) {
os.Exit(1) os.Exit(1)
} }
// If the file exists, it will be deleted
if _, err := os.Stat(fmt.Sprintf("%s/%s.json", dir, targetRole)); err == nil {
err = os.Remove(fmt.Sprintf("%s/%s.json", dir, targetRole))
if err != nil {
fmt.Printf("Error deleting existing role file: %v\n", err)
os.Exit(1)
}
}
roleFile := fmt.Sprintf("%s/%s.json", dir, targetRole) roleFile := fmt.Sprintf("%s/%s.json", dir, targetRole)
err = os.WriteFile(roleFile, roleData, 0600) err = os.WriteFile(roleFile, roleData, 0600)
if err != nil { if err != nil {
@@ -141,7 +143,7 @@ func assumeRole(args []string) {
os.Exit(1) os.Exit(1)
} }
fmt.Printf("Assumed role token saved to %s\n", roleFile) // fmt.Printf("Assumed role token saved to %s\n", roleFile)
} }
@@ -152,13 +154,13 @@ func roleAttachPolicy(args []string) {
var targetRole string var targetRole string
var useIdentity string var useIdentity string
roleattachpolicyCmd.StringVar(&targetRole, "role", "", "Target role (required)") roleattachpolicyCmd.StringVar(&targetRole, "rolename", "", "Target role (required)")
roleattachpolicyCmd.StringVar(&useIdentity, "identity", "", "Identity to use (default: default)") roleattachpolicyCmd.StringVar(&useIdentity, "identity", "", "Identity to use (default: default)")
roleattachpolicyCmd.Parse(args) roleattachpolicyCmd.Parse(args)
if targetRole == "" { if targetRole == "" {
fmt.Println("Error: either --role is required") fmt.Println("Error: either --rolename is required")
os.Exit(1) os.Exit(1)
} }
if useIdentity == "" { if useIdentity == "" {