|
| 1 | +package resourcemanager |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework-validators/setvalidator" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 15 | + "github.com/hashicorp/terraform-provider-google/google/fwtransport" |
| 16 | + "github.com/hashicorp/terraform-provider-google/google/fwutils" |
| 17 | + "github.com/hashicorp/terraform-provider-google/google/fwvalidators" |
| 18 | + "google.golang.org/api/iamcredentials/v1" |
| 19 | +) |
| 20 | + |
| 21 | +var _ ephemeral.EphemeralResource = &googleEphemeralServiceAccountJwt{} |
| 22 | + |
| 23 | +func GoogleEphemeralServiceAccountJwt() ephemeral.EphemeralResource { |
| 24 | + return &googleEphemeralServiceAccountJwt{} |
| 25 | +} |
| 26 | + |
| 27 | +type googleEphemeralServiceAccountJwt struct { |
| 28 | + providerConfig *fwtransport.FrameworkProviderConfig |
| 29 | +} |
| 30 | + |
| 31 | +func (p *googleEphemeralServiceAccountJwt) Metadata(ctx context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) { |
| 32 | + resp.TypeName = req.ProviderTypeName + "_service_account_jwt" |
| 33 | +} |
| 34 | + |
| 35 | +type ephemeralServiceAccountJwtModel struct { |
| 36 | + Payload types.String `tfsdk:"payload"` |
| 37 | + ExpiresIn types.Int64 `tfsdk:"expires_in"` |
| 38 | + TargetServiceAccount types.String `tfsdk:"target_service_account"` |
| 39 | + Delegates types.Set `tfsdk:"delegates"` |
| 40 | + Jwt types.String `tfsdk:"jwt"` |
| 41 | +} |
| 42 | + |
| 43 | +func (p *googleEphemeralServiceAccountJwt) Schema(ctx context.Context, req ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) { |
| 44 | + resp.Schema = schema.Schema{ |
| 45 | + Description: "Produces an arbitrary self-signed JWT for service accounts.", |
| 46 | + Attributes: map[string]schema.Attribute{ |
| 47 | + "payload": schema.StringAttribute{ |
| 48 | + Required: true, |
| 49 | + Description: `A JSON-encoded JWT claims set that will be included in the signed JWT.`, |
| 50 | + }, |
| 51 | + "expires_in": schema.Int64Attribute{ |
| 52 | + Optional: true, |
| 53 | + Description: "Number of seconds until the JWT expires. If set and non-zero an `exp` claim will be added to the payload derived from the current timestamp plus expires_in seconds.", |
| 54 | + Validators: []validator.Int64{ |
| 55 | + int64validator.AtLeast(1), // Must be greater than 0 |
| 56 | + }, |
| 57 | + }, |
| 58 | + "target_service_account": schema.StringAttribute{ |
| 59 | + Description: "The email of the service account that will sign the JWT.", |
| 60 | + Required: true, |
| 61 | + Validators: []validator.String{ |
| 62 | + fwvalidators.ServiceAccountEmailValidator{}, |
| 63 | + }, |
| 64 | + }, |
| 65 | + "delegates": schema.SetAttribute{ |
| 66 | + Description: "Delegate chain of approvals needed to perform full impersonation. Specify the fully qualified service account name.", |
| 67 | + Optional: true, |
| 68 | + ElementType: types.StringType, |
| 69 | + Validators: []validator.Set{ |
| 70 | + setvalidator.ValueStringsAre(fwvalidators.ServiceAccountEmailValidator{}), |
| 71 | + }, |
| 72 | + }, |
| 73 | + "jwt": schema.StringAttribute{ |
| 74 | + Description: "The signed JWT containing the JWT Claims Set from the `payload`.", |
| 75 | + Computed: true, |
| 76 | + Sensitive: true, |
| 77 | + }, |
| 78 | + }, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func (p *googleEphemeralServiceAccountJwt) Configure(ctx context.Context, req ephemeral.ConfigureRequest, resp *ephemeral.ConfigureResponse) { |
| 83 | + if req.ProviderData == nil { |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + pd, ok := req.ProviderData.(*fwtransport.FrameworkProviderConfig) |
| 88 | + if !ok { |
| 89 | + resp.Diagnostics.AddError( |
| 90 | + "Unexpected Data Source Configure Type", |
| 91 | + fmt.Sprintf("Expected *fwtransport.FrameworkProviderConfig, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 92 | + ) |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + p.providerConfig = pd |
| 97 | +} |
| 98 | + |
| 99 | +func (p *googleEphemeralServiceAccountJwt) Open(ctx context.Context, req ephemeral.OpenRequest, resp *ephemeral.OpenResponse) { |
| 100 | + var data ephemeralServiceAccountJwtModel |
| 101 | + |
| 102 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 103 | + |
| 104 | + payload := data.Payload.ValueString() |
| 105 | + |
| 106 | + if !data.ExpiresIn.IsNull() { |
| 107 | + expiresIn := data.ExpiresIn.ValueInt64() |
| 108 | + var decoded map[string]interface{} |
| 109 | + if err := json.Unmarshal([]byte(payload), &decoded); err != nil { |
| 110 | + resp.Diagnostics.AddError("Error decoding payload", err.Error()) |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + decoded["exp"] = time.Now().Add(time.Duration(expiresIn) * time.Second).Unix() |
| 115 | + |
| 116 | + payloadBytesWithExp, err := json.Marshal(decoded) |
| 117 | + if err != nil { |
| 118 | + resp.Diagnostics.AddError("Error re-encoding payload", err.Error()) |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + payload = string(payloadBytesWithExp) |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + name := fmt.Sprintf("projects/-/serviceAccounts/%s", data.TargetServiceAccount.ValueString()) |
| 127 | + |
| 128 | + service := p.providerConfig.NewIamCredentialsClient(p.providerConfig.UserAgent) |
| 129 | + jwtRequest := &iamcredentials.SignJwtRequest{ |
| 130 | + Payload: payload, |
| 131 | + Delegates: fwutils.StringSet(data.Delegates), |
| 132 | + } |
| 133 | + |
| 134 | + jwtResponse, err := service.Projects.ServiceAccounts.SignJwt(name, jwtRequest).Do() |
| 135 | + if err != nil { |
| 136 | + resp.Diagnostics.AddError("Error calling iamcredentials.SignJwt", err.Error()) |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + data.Jwt = types.StringValue(jwtResponse.SignedJwt) |
| 141 | + |
| 142 | + resp.Diagnostics.Append(resp.Result.Set(ctx, data)...) |
| 143 | +} |
0 commit comments