@@ -49,16 +49,29 @@ func (r *serviceAccountResource) Schema(_ context.Context, _ resource.SchemaRequ
49
49
Required : true ,
50
50
ElementType : types .StringType ,
51
51
},
52
+ // Agent service account
52
53
"public_key" : schema.StringAttribute {
53
- Required : true ,
54
+ Optional : true ,
54
55
},
55
- /*
56
- "privateKey": schema.StringAttribute{
57
- Required: true,
58
- },
59
- */
60
56
"credential_lifetime" : schema.Int32Attribute {
61
- Required : true ,
57
+ Optional : true ,
58
+ },
59
+ // Issuer service account (jwks)
60
+ "jwks_uri" : schema.StringAttribute {
61
+ Optional : true ,
62
+ },
63
+ "issuer_url" : schema.StringAttribute {
64
+ Optional : true ,
65
+ },
66
+ "audience" : schema.StringAttribute {
67
+ Optional : true ,
68
+ },
69
+ "subject" : schema.StringAttribute {
70
+ Optional : true ,
71
+ },
72
+ "applications" : schema.SetAttribute {
73
+ Optional : true ,
74
+ ElementType : types .StringType ,
62
75
},
63
76
},
64
77
}
@@ -84,13 +97,17 @@ func (r *serviceAccountResource) Configure(_ context.Context, req resource.Confi
84
97
}
85
98
86
99
type serviceAccountResourceModel struct {
87
- ID types.String `tfsdk:"id"`
88
- Name types.String `tfsdk:"name"`
89
- Owner types.String `tfsdk:"owner"`
90
- Scopes []types.String `tfsdk:"scopes"`
91
- PublicKey types.String `tfsdk:"public_key"`
92
- //PrivateKey types.String `tfsdk:"privateKey"`
93
- CredentialLifetime types.Int32 `tfsdk:"credential_lifetime"`
100
+ ID types.String `tfsdk:"id"`
101
+ Name types.String `tfsdk:"name"`
102
+ Owner types.String `tfsdk:"owner"`
103
+ Scopes []types.String `tfsdk:"scopes"`
104
+ PublicKey types.String `tfsdk:"public_key"`
105
+ CredentialLifetime types.Int32 `tfsdk:"credential_lifetime"`
106
+ JwksURI types.String `tfsdk:"jwks_uri"`
107
+ IssuerURL types.String `tfsdk:"issuer_url"`
108
+ Audience types.String `tfsdk:"audience"`
109
+ Subject types.String `tfsdk:"subject"`
110
+ Applications []types.String `tfsdk:"applications"`
94
111
}
95
112
96
113
func (r * serviceAccountResource ) Create (ctx context.Context , req resource.CreateRequest , resp * resource.CreateResponse ) {
@@ -106,12 +123,48 @@ func (r *serviceAccountResource) Create(ctx context.Context, req resource.Create
106
123
}
107
124
108
125
serviceAccount := tlspc.ServiceAccount {
109
- Name : plan .Name .ValueString (),
110
- Owner : plan .Owner .ValueString (),
111
- Scopes : scopes ,
112
- PublicKey : plan .PublicKey .ValueString (),
113
- CredentialLifetime : plan .CredentialLifetime .ValueInt32 (),
114
- AuthenticationType : "rsaKey" ,
126
+ Name : plan .Name .ValueString (),
127
+ Owner : plan .Owner .ValueString (),
128
+ Scopes : scopes ,
129
+ }
130
+
131
+ configured := false
132
+ // Agent type
133
+ if plan .PublicKey .ValueString () != "" || plan .CredentialLifetime .ValueInt32 () > 0 {
134
+ serviceAccount .PublicKey = plan .PublicKey .ValueString ()
135
+ serviceAccount .CredentialLifetime = plan .CredentialLifetime .ValueInt32 ()
136
+ serviceAccount .AuthenticationType = "rsaKey"
137
+ configured = true
138
+ }
139
+
140
+ // Issuer type
141
+ if plan .JwksURI .ValueString () != "" || plan .IssuerURL .ValueString () != "" || plan .Audience .ValueString () != "" || plan .Subject .ValueString () != "" || len (plan .Applications ) > 0 {
142
+ if serviceAccount .AuthenticationType == "rsaKey" {
143
+ resp .Diagnostics .AddError (
144
+ "Error creating serviceAccount" ,
145
+ "Could not create serviceAccount, invalid configuration (both public_key and jwks fields present)" ,
146
+ )
147
+ return
148
+ }
149
+ serviceAccount .JwksURI = plan .JwksURI .ValueString ()
150
+ serviceAccount .IssuerURL = plan .IssuerURL .ValueString ()
151
+ serviceAccount .Audience = plan .Audience .ValueString ()
152
+ serviceAccount .Subject = plan .Subject .ValueString ()
153
+ serviceAccount .AuthenticationType = "rsaKeyFederated"
154
+
155
+ apps := []string {}
156
+ for _ , v := range plan .Applications {
157
+ apps = append (apps , v .ValueString ())
158
+ }
159
+ serviceAccount .Applications = apps
160
+ configured = true
161
+ }
162
+ if ! configured {
163
+ resp .Diagnostics .AddError (
164
+ "Error creating serviceAccount" ,
165
+ "Could not create serviceAccount, invalid configuration (neither public_key or jwks fields present)" ,
166
+ )
167
+ return
115
168
}
116
169
117
170
created , err := r .client .CreateServiceAccount (serviceAccount )
@@ -180,13 +233,49 @@ func (r *serviceAccountResource) Update(ctx context.Context, req resource.Update
180
233
}
181
234
182
235
serviceAccount := tlspc.ServiceAccount {
183
- ID : state .ID .ValueString (),
184
- Name : plan .Name .ValueString (),
185
- Owner : plan .Owner .ValueString (),
186
- Scopes : scopes ,
187
- PublicKey : plan .PublicKey .ValueString (),
188
- CredentialLifetime : plan .CredentialLifetime .ValueInt32 (),
189
- AuthenticationType : "rsaKey" ,
236
+ ID : state .ID .ValueString (),
237
+ Name : plan .Name .ValueString (),
238
+ Owner : plan .Owner .ValueString (),
239
+ Scopes : scopes ,
240
+ }
241
+
242
+ configured := false
243
+ // Agent type
244
+ if plan .PublicKey .ValueString () != "" || plan .CredentialLifetime .ValueInt32 () > 0 {
245
+ serviceAccount .PublicKey = plan .PublicKey .ValueString ()
246
+ serviceAccount .CredentialLifetime = plan .CredentialLifetime .ValueInt32 ()
247
+ serviceAccount .AuthenticationType = "rsaKey"
248
+ configured = true
249
+ }
250
+
251
+ // Issuer type
252
+ if plan .JwksURI .ValueString () != "" || plan .IssuerURL .ValueString () != "" || plan .Audience .ValueString () != "" || plan .Subject .ValueString () != "" || len (plan .Applications ) > 0 {
253
+ if serviceAccount .AuthenticationType == "rsaKey" {
254
+ resp .Diagnostics .AddError (
255
+ "Error creating serviceAccount" ,
256
+ "Could not create serviceAccount, invalid configuration (both public_key and jwks fields present)" ,
257
+ )
258
+ return
259
+ }
260
+ serviceAccount .JwksURI = plan .JwksURI .ValueString ()
261
+ serviceAccount .IssuerURL = plan .IssuerURL .ValueString ()
262
+ serviceAccount .Audience = plan .Audience .ValueString ()
263
+ serviceAccount .Subject = plan .Subject .ValueString ()
264
+ serviceAccount .AuthenticationType = "rsaKeyFederated"
265
+
266
+ apps := []string {}
267
+ for _ , v := range plan .Applications {
268
+ apps = append (apps , v .ValueString ())
269
+ }
270
+ serviceAccount .Applications = apps
271
+ configured = true
272
+ }
273
+ if ! configured {
274
+ resp .Diagnostics .AddError (
275
+ "Error creating serviceAccount" ,
276
+ "Could not create serviceAccount, invalid configuration (neither public_key or jwks fields present)" ,
277
+ )
278
+ return
190
279
}
191
280
192
281
err := r .client .UpdateServiceAccount (serviceAccount )
0 commit comments