|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-provider-azuread/internal/tf/pluginsdk" |
| 11 | +) |
| 12 | + |
| 13 | +// logEntry avoids log entries showing up in test output |
| 14 | +func logEntry(f string, v ...interface{}) { |
| 15 | + if os.Getenv("TF_LOG") == "" { |
| 16 | + return |
| 17 | + } |
| 18 | + |
| 19 | + if os.Getenv("TF_ACC") != "" { |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + log.Printf(f, v...) |
| 24 | +} |
| 25 | + |
| 26 | +func decodeCertificate(clientCertificate string) ([]byte, error) { |
| 27 | + var pfx []byte |
| 28 | + if clientCertificate != "" { |
| 29 | + out := make([]byte, base64.StdEncoding.DecodedLen(len(clientCertificate))) |
| 30 | + n, err := base64.StdEncoding.Decode(out, []byte(clientCertificate)) |
| 31 | + if err != nil { |
| 32 | + return pfx, fmt.Errorf("could not decode client certificate data: %v", err) |
| 33 | + } |
| 34 | + pfx = out[:n] |
| 35 | + } |
| 36 | + return pfx, nil |
| 37 | +} |
| 38 | + |
| 39 | +func getOidcToken(d *pluginsdk.ResourceData) (*string, error) { |
| 40 | + idToken := d.Get("oidc_token").(string) |
| 41 | + |
| 42 | + if path := d.Get("oidc_token_file_path").(string); path != "" { |
| 43 | + fileTokenRaw, err := os.ReadFile(path) |
| 44 | + |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("reading OIDC Token from file %q: %v", path, err) |
| 47 | + } |
| 48 | + |
| 49 | + fileToken := strings.TrimSpace(string(fileTokenRaw)) |
| 50 | + |
| 51 | + if idToken != "" && idToken != fileToken { |
| 52 | + return nil, fmt.Errorf("mismatch between supplied OIDC token and supplied OIDC token file contents - please either remove one or ensure they match") |
| 53 | + } |
| 54 | + |
| 55 | + idToken = fileToken |
| 56 | + } |
| 57 | + |
| 58 | + return &idToken, nil |
| 59 | +} |
| 60 | + |
| 61 | +func getClientId(d *pluginsdk.ResourceData) (*string, error) { |
| 62 | + clientId := strings.TrimSpace(d.Get("client_id").(string)) |
| 63 | + |
| 64 | + if path := d.Get("client_id_file_path").(string); path != "" { |
| 65 | + fileClientIdRaw, err := os.ReadFile(path) |
| 66 | + |
| 67 | + if err != nil { |
| 68 | + return nil, fmt.Errorf("reading Client ID from file %q: %v", path, err) |
| 69 | + } |
| 70 | + |
| 71 | + fileClientId := strings.TrimSpace(string(fileClientIdRaw)) |
| 72 | + |
| 73 | + if clientId != "" && clientId != fileClientId { |
| 74 | + return nil, fmt.Errorf("mismatch between supplied Client ID and supplied Client ID file contents - please either remove one or ensure they match") |
| 75 | + } |
| 76 | + |
| 77 | + clientId = fileClientId |
| 78 | + } |
| 79 | + |
| 80 | + return &clientId, nil |
| 81 | +} |
| 82 | + |
| 83 | +func getClientSecret(d *pluginsdk.ResourceData) (*string, error) { |
| 84 | + clientSecret := strings.TrimSpace(d.Get("client_secret").(string)) |
| 85 | + |
| 86 | + if path := d.Get("client_secret_file_path").(string); path != "" { |
| 87 | + fileSecretRaw, err := os.ReadFile(path) |
| 88 | + |
| 89 | + if err != nil { |
| 90 | + return nil, fmt.Errorf("reading Client Secret from file %q: %v", path, err) |
| 91 | + } |
| 92 | + |
| 93 | + fileSecret := strings.TrimSpace(string(fileSecretRaw)) |
| 94 | + |
| 95 | + if clientSecret != "" && clientSecret != fileSecret { |
| 96 | + return nil, fmt.Errorf("mismatch between supplied Client Secret and supplied Client Secret file contents - please either remove one or ensure they match") |
| 97 | + } |
| 98 | + |
| 99 | + clientSecret = fileSecret |
| 100 | + } |
| 101 | + |
| 102 | + return &clientSecret, nil |
| 103 | +} |
| 104 | + |
| 105 | +func getTenantId(d *pluginsdk.ResourceData) (*string, error) { |
| 106 | + tenantId := strings.TrimSpace(d.Get("tenant_id").(string)) |
| 107 | + |
| 108 | + if d.Get("use_aks_workload_identity").(bool) && os.Getenv("AZURE_TENANT_ID") != "" { |
| 109 | + aksTenantId := os.Getenv("AZURE_TENANT_ID") |
| 110 | + if tenantId != "" && tenantId != aksTenantId { |
| 111 | + return nil, fmt.Errorf("mismatch between supplied Tenant ID and that provided by AKS Workload Identity - please remove, ensure they match, or disable use_aks_workload_identity") |
| 112 | + } |
| 113 | + tenantId = aksTenantId |
| 114 | + } |
| 115 | + |
| 116 | + return &tenantId, nil |
| 117 | +} |
0 commit comments