|
| 1 | +package access_token |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 13 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 14 | + "github.com/stackitcloud/stackit-sdk-go/core/clients" |
| 15 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" |
| 16 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" |
| 17 | +) |
| 18 | + |
| 19 | +// #nosec G101 tokenUrl is a public endpoint, not a hardcoded credential |
| 20 | +const tokenUrl = "https://service-account.api.stackit.cloud/token" |
| 21 | + |
| 22 | +var ( |
| 23 | + _ ephemeral.EphemeralResource = &accessTokenEphemeralResource{} |
| 24 | + _ ephemeral.EphemeralResourceWithConfigure = &accessTokenEphemeralResource{} |
| 25 | +) |
| 26 | + |
| 27 | +func NewAccessTokenEphemeralResource() ephemeral.EphemeralResource { |
| 28 | + return &accessTokenEphemeralResource{} |
| 29 | +} |
| 30 | + |
| 31 | +type accessTokenEphemeralResource struct { |
| 32 | + serviceAccountKeyPath string |
| 33 | + serviceAccountKey string |
| 34 | + privateKeyPath string |
| 35 | + privateKey string |
| 36 | +} |
| 37 | + |
| 38 | +func (e *accessTokenEphemeralResource) Configure(ctx context.Context, req ephemeral.ConfigureRequest, resp *ephemeral.ConfigureResponse) { |
| 39 | + providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) |
| 40 | + if !ok { |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + e.serviceAccountKey = providerData.ServiceAccountKey |
| 45 | + e.serviceAccountKeyPath = providerData.ServiceAccountKeyPath |
| 46 | + e.privateKey = providerData.PrivateKey |
| 47 | + e.privateKeyPath = providerData.PrivateKeyPath |
| 48 | +} |
| 49 | + |
| 50 | +type ephemeralTokenModel struct { |
| 51 | + AccessToken types.String `tfsdk:"access_token"` |
| 52 | +} |
| 53 | + |
| 54 | +func (e *accessTokenEphemeralResource) Metadata(_ context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) { |
| 55 | + resp.TypeName = req.ProviderTypeName + "_access_token" |
| 56 | +} |
| 57 | + |
| 58 | +func (e *accessTokenEphemeralResource) Schema(_ context.Context, _ ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) { |
| 59 | + resp.Schema = schema.Schema{ |
| 60 | + Description: "STACKIT Access Token ephemeral resource schema.", |
| 61 | + Attributes: map[string]schema.Attribute{ |
| 62 | + "access_token": schema.StringAttribute{ |
| 63 | + Description: "JWT access token for STACKIT API authentication.", |
| 64 | + Computed: true, |
| 65 | + Sensitive: true, |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func (e *accessTokenEphemeralResource) Open(ctx context.Context, req ephemeral.OpenRequest, resp *ephemeral.OpenResponse) { |
| 72 | + var model ephemeralTokenModel |
| 73 | + |
| 74 | + resp.Diagnostics.Append(req.Config.Get(ctx, &model)...) |
| 75 | + if resp.Diagnostics.HasError() { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + serviceAccountKey, diags := loadServiceAccountKey(ctx, e.serviceAccountKey, e.serviceAccountKeyPath) |
| 80 | + resp.Diagnostics.Append(diags...) |
| 81 | + if resp.Diagnostics.HasError() { |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + privateKey, diags := resolvePrivateKey(ctx, e.privateKey, e.privateKeyPath, serviceAccountKey) |
| 86 | + resp.Diagnostics.Append(diags...) |
| 87 | + if resp.Diagnostics.HasError() { |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + client, diags := initKeyFlowClient(ctx, serviceAccountKey, privateKey) |
| 92 | + resp.Diagnostics.Append(diags...) |
| 93 | + if resp.Diagnostics.HasError() { |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + accessToken, err := client.GetAccessToken() |
| 98 | + if err != nil { |
| 99 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Access token generation failed", fmt.Sprintf("Error generating access token: %v", err)) |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + ctx = tflog.SetField(ctx, "access_token", accessToken) |
| 104 | + model.AccessToken = types.StringValue(accessToken) |
| 105 | + resp.Diagnostics.Append(resp.Result.Set(ctx, model)...) |
| 106 | +} |
| 107 | + |
| 108 | +// loadServiceAccountKey loads the service account key based on env vars, or fallback to provider config. |
| 109 | +func loadServiceAccountKey(ctx context.Context, cfgValue, cfgPath string) (*clients.ServiceAccountKeyResponse, diag.Diagnostics) { |
| 110 | + var diags diag.Diagnostics |
| 111 | + |
| 112 | + env := os.Getenv("STACKIT_SERVICE_ACCOUNT_KEY") |
| 113 | + envPath := os.Getenv("STACKIT_SERVICE_ACCOUNT_KEY_PATH") |
| 114 | + |
| 115 | + var data []byte |
| 116 | + switch { |
| 117 | + case env != "": |
| 118 | + data = []byte(env) |
| 119 | + case envPath != "": |
| 120 | + b, err := os.ReadFile(envPath) |
| 121 | + if err != nil { |
| 122 | + core.LogAndAddError(ctx, &diags, "Failed to read service account key file (env path)", fmt.Sprintf("Error reading key file: %v", err)) |
| 123 | + return nil, diags |
| 124 | + } |
| 125 | + data = b |
| 126 | + case cfgValue != "": |
| 127 | + data = []byte(cfgValue) |
| 128 | + case cfgPath != "": |
| 129 | + b, err := os.ReadFile(cfgPath) |
| 130 | + if err != nil { |
| 131 | + core.LogAndAddError(ctx, &diags, "Failed to read service account key file (provider path)", fmt.Sprintf("Error reading key file: %v", err)) |
| 132 | + return nil, diags |
| 133 | + } |
| 134 | + data = b |
| 135 | + default: |
| 136 | + core.LogAndAddError(ctx, &diags, "Missing service account key", "Neither STACKIT_SERVICE_ACCOUNT_KEY, STACKIT_SERVICE_ACCOUNT_KEY_PATH, provider value, nor path were provided.") |
| 137 | + return nil, diags |
| 138 | + } |
| 139 | + |
| 140 | + var key clients.ServiceAccountKeyResponse |
| 141 | + if err := json.Unmarshal(data, &key); err != nil { |
| 142 | + core.LogAndAddError(ctx, &diags, "Failed to parse service account key", fmt.Sprintf("Unmarshal error: %v", err)) |
| 143 | + return nil, diags |
| 144 | + } |
| 145 | + |
| 146 | + return &key, diags |
| 147 | +} |
| 148 | + |
| 149 | +// resolvePrivateKey determines the private key value using env, conf, fallbacks. |
| 150 | +func resolvePrivateKey(ctx context.Context, cfgValue, cfgPath string, key *clients.ServiceAccountKeyResponse) (string, diag.Diagnostics) { |
| 151 | + var diags diag.Diagnostics |
| 152 | + |
| 153 | + env := os.Getenv("STACKIT_PRIVATE_KEY") |
| 154 | + envPath := os.Getenv("STACKIT_PRIVATE_KEY_PATH") |
| 155 | + |
| 156 | + switch { |
| 157 | + case env != "": |
| 158 | + return env, diags |
| 159 | + case envPath != "": |
| 160 | + content, err := os.ReadFile(envPath) |
| 161 | + if err != nil { |
| 162 | + core.LogAndAddError(ctx, &diags, "Failed to read private key file (env path)", fmt.Sprintf("Error: %v", err)) |
| 163 | + return "", diags |
| 164 | + } |
| 165 | + return string(content), diags |
| 166 | + case cfgValue != "": |
| 167 | + return cfgValue, diags |
| 168 | + case cfgPath != "": |
| 169 | + content, err := os.ReadFile(cfgPath) |
| 170 | + if err != nil { |
| 171 | + core.LogAndAddError(ctx, &diags, "Failed to read private key file (provider path)", fmt.Sprintf("Error: %v", err)) |
| 172 | + return "", diags |
| 173 | + } |
| 174 | + return string(content), diags |
| 175 | + case key.Credentials != nil && key.Credentials.PrivateKey != nil: |
| 176 | + return *key.Credentials.PrivateKey, diags |
| 177 | + default: |
| 178 | + core.LogAndAddError(ctx, &diags, "Missing private key", "No private key set via env, provider, or service account credentials.") |
| 179 | + return "", diags |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +// initKeyFlowClient configures and initializes a new KeyFlow client using the key and private key. |
| 184 | +func initKeyFlowClient(ctx context.Context, key *clients.ServiceAccountKeyResponse, privateKey string) (*clients.KeyFlow, diag.Diagnostics) { |
| 185 | + var diags diag.Diagnostics |
| 186 | + |
| 187 | + client := &clients.KeyFlow{} |
| 188 | + cfg := &clients.KeyFlowConfig{ |
| 189 | + ServiceAccountKey: key, |
| 190 | + PrivateKey: privateKey, |
| 191 | + TokenUrl: tokenUrl, |
| 192 | + } |
| 193 | + |
| 194 | + if err := client.Init(cfg); err != nil { |
| 195 | + core.LogAndAddError(ctx, &diags, "Failed to initialize KeyFlow", fmt.Sprintf("KeyFlow client init error: %v", err)) |
| 196 | + return nil, diags |
| 197 | + } |
| 198 | + |
| 199 | + return client, diags |
| 200 | +} |
0 commit comments