Skip to content

Commit 1f5abc3

Browse files
authored
service-accounts: add jwts, better handling of user id (#26)
1 parent 0c07f47 commit 1f5abc3

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

internal/provider/models.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package provider
22

33
import (
44
"context"
5+
"strings"
56
"time"
67

78
"github.com/hashicorp/terraform-plugin-framework/diag"
89
"github.com/hashicorp/terraform-plugin-framework/types"
10+
911
"github.com/pomerium/enterprise-client-go/pb"
1012
)
1113

@@ -17,31 +19,34 @@ type ServiceAccountModel struct {
1719
Description types.String `tfsdk:"description"`
1820
UserID types.String `tfsdk:"user_id"`
1921
ExpiresAt types.String `tfsdk:"expires_at"`
22+
JWT types.String `tfsdk:"jwt"`
2023
}
2124

2225
func ConvertServiceAccountToPB(_ context.Context, src *ServiceAccountResourceModel) (*pb.PomeriumServiceAccount, diag.Diagnostics) {
23-
var diagnostics diag.Diagnostics
26+
var diags diag.Diagnostics
2427

25-
namespaceID := src.NamespaceID.ValueString()
2628
pbServiceAccount := &pb.PomeriumServiceAccount{
27-
Id: src.ID.ValueString(),
28-
UserId: src.Name.ValueString(),
29-
NamespaceId: &namespaceID,
29+
Id: src.ID.ValueString(),
30+
UserId: src.Name.ValueString(),
31+
}
32+
33+
if src.NamespaceID.ValueString() != "" {
34+
pbServiceAccount.NamespaceId = src.NamespaceID.ValueStringPointer()
3035
}
3136

3237
if !src.Description.IsNull() {
3338
desc := src.Description.ValueString()
3439
pbServiceAccount.Description = &desc
3540
}
3641

37-
return pbServiceAccount, diagnostics
42+
return pbServiceAccount, diags
3843
}
3944

4045
func ConvertServiceAccountFromPB(dst *ServiceAccountResourceModel, src *pb.PomeriumServiceAccount) diag.Diagnostics {
4146
var diagnostics diag.Diagnostics
4247

4348
dst.ID = types.StringValue(src.Id)
44-
dst.Name = types.StringValue(src.UserId)
49+
dst.Name = types.StringValue(strings.TrimSuffix(src.UserId, "@"+src.GetNamespaceId()+".pomerium"))
4550
if src.NamespaceId != nil {
4651
dst.NamespaceID = types.StringValue(*src.NamespaceId)
4752
} else {

internal/provider/service_account.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,40 @@ func (r *ServiceAccountResource) Schema(_ context.Context, _ resource.SchemaRequ
5050
},
5151
"name": schema.StringAttribute{
5252
Description: "Name of the service account.",
53-
Required: true,
53+
Optional: true,
54+
Computed: true,
55+
PlanModifiers: []planmodifier.String{
56+
stringplanmodifier.RequiresReplace(),
57+
},
5458
},
5559
"namespace_id": schema.StringAttribute{
5660
Description: "ID of the namespace the service account belongs to.",
57-
Required: true,
61+
Optional: true,
62+
Computed: true,
63+
PlanModifiers: []planmodifier.String{
64+
stringplanmodifier.RequiresReplace(),
65+
},
5866
},
5967
"description": schema.StringAttribute{
6068
Description: "Description of the service account.",
6169
Optional: true,
6270
},
6371
"user_id": schema.StringAttribute{
64-
Computed: true,
6572
Description: "User ID associated with the service account.",
73+
Computed: true,
6674
},
6775
"expires_at": schema.StringAttribute{
6876
Computed: true,
6977
Description: "Timestamp when the service account expires.",
7078
},
79+
"jwt": schema.StringAttribute{
80+
Computed: true,
81+
Sensitive: true,
82+
Description: "The Service Account JWT used for authentication. This is only populated when creating a new service account.",
83+
PlanModifiers: []planmodifier.String{
84+
stringplanmodifier.UseStateForUnknown(),
85+
},
86+
},
7187
},
7288
}
7389
}
@@ -111,7 +127,13 @@ func (r *ServiceAccountResource) Create(ctx context.Context, req resource.Create
111127
return
112128
}
113129

114-
plan.ID = types.StringValue(respServiceAccount.ServiceAccount.Id)
130+
diags = ConvertServiceAccountFromPB(&plan, respServiceAccount.ServiceAccount)
131+
resp.Diagnostics.Append(diags...)
132+
if resp.Diagnostics.HasError() {
133+
return
134+
}
135+
136+
plan.JWT = types.StringValue(respServiceAccount.JWT)
115137

116138
tflog.Trace(ctx, "Created a service account", map[string]interface{}{
117139
"id": plan.ID.ValueString(),

0 commit comments

Comments
 (0)