|
| 1 | +package akamai |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/1Password/shell-plugins/sdk" |
| 7 | + "github.com/1Password/shell-plugins/sdk/importer" |
| 8 | + "github.com/1Password/shell-plugins/sdk/provision" |
| 9 | + "github.com/1Password/shell-plugins/sdk/schema" |
| 10 | + "github.com/1Password/shell-plugins/sdk/schema/credname" |
| 11 | + "github.com/1Password/shell-plugins/sdk/schema/fieldname" |
| 12 | +) |
| 13 | + |
| 14 | +func APIClientCredentials() schema.CredentialType { |
| 15 | + return schema.CredentialType{ |
| 16 | + Name: credname.APIClientCredentials, |
| 17 | + DocsURL: sdk.URL("https://techdocs.akamai.com/developer/docs/set-up-authentication-credentials"), |
| 18 | + ManagementURL: sdk.URL("https://control.akamai.com/apps/identity-management/#/tabs/users/list"), |
| 19 | + Fields: []schema.CredentialField{ |
| 20 | + { |
| 21 | + Name: fieldname.ClientSecret, |
| 22 | + MarkdownDescription: "Client Secret used to authenticate to Akamai.", |
| 23 | + Secret: true, |
| 24 | + Composition: &schema.ValueComposition{ |
| 25 | + Length: 44, |
| 26 | + Charset: schema.Charset{ |
| 27 | + Uppercase: true, |
| 28 | + Lowercase: true, |
| 29 | + Digits: true, |
| 30 | + Symbols: true, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + Name: fieldname.Host, |
| 36 | + MarkdownDescription: "Hostname used to authenticate to Akamai APIs.", |
| 37 | + Secret: false, |
| 38 | + Composition: &schema.ValueComposition{ |
| 39 | + Length: 58, |
| 40 | + Charset: schema.Charset{ |
| 41 | + Lowercase: true, |
| 42 | + Digits: true, |
| 43 | + Specific: []rune{45, 46}, // "-" and "." |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + Name: fieldname.AccessToken, |
| 49 | + MarkdownDescription: "Access Token used to authenticate to Akamai.", |
| 50 | + Secret: false, |
| 51 | + Composition: &schema.ValueComposition{ |
| 52 | + Length: 38, |
| 53 | + Charset: schema.Charset{ |
| 54 | + Lowercase: true, |
| 55 | + Digits: true, |
| 56 | + Specific: []rune{45}, // "-" |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + Name: fieldname.ClientToken, |
| 62 | + MarkdownDescription: "Client Token used to authenticate to Akamai.", |
| 63 | + Secret: false, |
| 64 | + Composition: &schema.ValueComposition{ |
| 65 | + Length: 38, |
| 66 | + Charset: schema.Charset{ |
| 67 | + Lowercase: true, |
| 68 | + Digits: true, |
| 69 | + Specific: []rune{45}, // "-" |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + DefaultProvisioner: provision.TempFile(configFile, |
| 75 | + provision.Filename(".edgerc"), |
| 76 | + provision.AddArgs( |
| 77 | + "--edgerc", "{{ .Path }}", |
| 78 | + "--section", "default", |
| 79 | + ), |
| 80 | + provision.SetPathAsEnvVar("EDGERC"), // for Akamai Terraform provider |
| 81 | + ), |
| 82 | + Importer: importer.TryAll( |
| 83 | + TryAkamaiConfigFile(), |
| 84 | + )} |
| 85 | +} |
| 86 | + |
| 87 | +func configFile(in sdk.ProvisionInput) ([]byte, error) { |
| 88 | + contents := "[default]\n" |
| 89 | + |
| 90 | + if clientsecret, ok := in.ItemFields[fieldname.ClientSecret]; ok { |
| 91 | + contents += "client_secret = " + clientsecret + "\n" |
| 92 | + } |
| 93 | + |
| 94 | + if host, ok := in.ItemFields[fieldname.Host]; ok { |
| 95 | + contents += "host = " + host + "\n" |
| 96 | + } |
| 97 | + |
| 98 | + if accesstoken, ok := in.ItemFields[fieldname.AccessToken]; ok { |
| 99 | + contents += "access_token = " + accesstoken + "\n" |
| 100 | + } |
| 101 | + |
| 102 | + if clienttoken, ok := in.ItemFields[fieldname.ClientToken]; ok { |
| 103 | + contents += "client_token = " + clienttoken + "\n" |
| 104 | + } |
| 105 | + |
| 106 | + return []byte(contents), nil |
| 107 | +} |
| 108 | + |
| 109 | +// Load credentials from the ~/.edgerc file. |
| 110 | +// Import separate credentials into 1Password based on the sections in the ~/.edgerc file. |
| 111 | +func TryAkamaiConfigFile() sdk.Importer { |
| 112 | + return importer.TryFile("~/.edgerc", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { |
| 113 | + configFile, err := contents.ToINI() |
| 114 | + if err != nil { |
| 115 | + out.AddError(err) |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + for _, section := range configFile.Sections() { |
| 120 | + profileName := section.Name() |
| 121 | + |
| 122 | + fields := make(map[sdk.FieldName]string) |
| 123 | + if section.HasKey("client_secret") && section.Key("client_secret").Value() != "" { |
| 124 | + fields[fieldname.ClientSecret] = section.Key("client_secret").Value() |
| 125 | + } |
| 126 | + if section.HasKey("host") && section.Key("host").Value() != "" { |
| 127 | + fields[fieldname.Host] = section.Key("host").Value() |
| 128 | + } |
| 129 | + if section.HasKey("access_token") && section.Key("access_token").Value() != "" { |
| 130 | + fields[fieldname.AccessToken] = section.Key("access_token").Value() |
| 131 | + } |
| 132 | + if section.HasKey("client_token") && section.Key("client_token").Value() != "" { |
| 133 | + fields[fieldname.ClientToken] = section.Key("client_token").Value() |
| 134 | + } |
| 135 | + |
| 136 | + // add candidates that contain all required credential fields |
| 137 | + if fields[fieldname.ClientSecret] != "" && fields[fieldname.Host] != "" && fields[fieldname.AccessToken] != "" && fields[fieldname.ClientToken] != "" { |
| 138 | + out.AddCandidate(sdk.ImportCandidate{ |
| 139 | + NameHint: importer.SanitizeNameHint(profileName), |
| 140 | + Fields: fields, |
| 141 | + }) |
| 142 | + } |
| 143 | + } |
| 144 | + }) |
| 145 | +} |
0 commit comments