Skip to content

Commit b0b566d

Browse files
committed
add originator id
1 parent 3cd77d8 commit b0b566d

11 files changed

+333
-43
lines changed

internal/provider/keychain.go internal/provider/key_chain.go

+3-25
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,8 @@ func (r *KeyChainResource) Create(ctx context.Context, req resource.CreateReques
9494
return
9595
}
9696

97-
keyPairReq := &pb.CreateKeyPairRequest{
98-
NamespaceId: plan.NamespaceID.ValueString(),
99-
Name: plan.Name.ValueString(),
100-
Format: pb.Format_PEM,
101-
Certificate: []byte(plan.Certificate.ValueString()),
102-
}
103-
104-
if !plan.Key.IsNull() {
105-
keyData := []byte(plan.Key.ValueString())
106-
keyPairReq.Key = keyData
107-
}
108-
109-
respKeyPair, err := r.client.KeyChainService.CreateKeyPair(ctx, keyPairReq)
97+
createReq := ConvertKeyPairToCreatePB(&plan)
98+
respKeyPair, err := r.client.KeyChainService.CreateKeyPair(ctx, createReq)
11099
if err != nil {
111100
resp.Diagnostics.AddError("Error creating key pair", err.Error())
112101
return
@@ -157,18 +146,7 @@ func (r *KeyChainResource) Update(ctx context.Context, req resource.UpdateReques
157146
return
158147
}
159148

160-
fmt := pb.Format_PEM
161-
updateReq := &pb.UpdateKeyPairRequest{
162-
Id: plan.ID.ValueString(),
163-
Name: plan.Name.ValueStringPointer(),
164-
Format: &fmt,
165-
Certificate: []byte(plan.Certificate.ValueString()),
166-
}
167-
168-
if !plan.Key.IsNull() {
169-
updateReq.Key = []byte(plan.Key.ValueString())
170-
}
171-
149+
updateReq := ConvertKeyPairToUpdatePB(&plan)
172150
_, err := r.client.KeyChainService.UpdateKeyPair(ctx, updateReq)
173151
if err != nil {
174152
resp.Diagnostics.AddError("Error updating key pair", err.Error())

internal/provider/key_chain_model.go

+35-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,45 @@ package provider
22

33
import (
44
"github.com/hashicorp/terraform-plugin-framework/types"
5+
6+
"github.com/pomerium/enterprise-client-go/pb"
57
)
68

79
type KeyPairModel struct {
10+
Certificate types.String `tfsdk:"certificate"`
811
ID types.String `tfsdk:"id"`
12+
Key types.String `tfsdk:"key"`
913
Name types.String `tfsdk:"name"`
1014
NamespaceID types.String `tfsdk:"namespace_id"`
11-
Certificate types.String `tfsdk:"certificate"`
12-
Key types.String `tfsdk:"key"`
15+
}
16+
17+
func ConvertKeyPairToCreatePB(src *KeyPairModel) *pb.CreateKeyPairRequest {
18+
dst := &pb.CreateKeyPairRequest{
19+
OriginatorId: originatorID,
20+
NamespaceId: src.NamespaceID.ValueString(),
21+
Name: src.Name.ValueString(),
22+
Format: pb.Format_PEM,
23+
Certificate: []byte(src.Certificate.ValueString()),
24+
}
25+
if !src.Key.IsNull() {
26+
keyData := []byte(src.Key.ValueString())
27+
dst.Key = keyData
28+
}
29+
return dst
30+
}
31+
32+
func ConvertKeyPairToUpdatePB(src *KeyPairModel) *pb.UpdateKeyPairRequest {
33+
fmt := pb.Format_PEM
34+
dst := &pb.UpdateKeyPairRequest{
35+
OriginatorId: originatorID,
36+
Id: src.ID.ValueString(),
37+
Name: src.Name.ValueStringPointer(),
38+
Format: &fmt,
39+
Certificate: []byte(src.Certificate.ValueString()),
40+
}
41+
if !src.Key.IsNull() {
42+
keyData := []byte(src.Key.ValueString())
43+
dst.Key = keyData
44+
}
45+
return dst
1346
}
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package provider_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
7+
"github.com/hashicorp/terraform-plugin-framework/types"
8+
"google.golang.org/protobuf/proto"
9+
"google.golang.org/protobuf/testing/protocmp"
10+
11+
"github.com/pomerium/enterprise-client-go/pb"
12+
"github.com/pomerium/enterprise-terraform-provider/internal/provider"
13+
)
14+
15+
func TestConvertKeyPairToCreatePB(t *testing.T) {
16+
t.Parallel()
17+
18+
expected := &pb.CreateKeyPairRequest{
19+
Certificate: []byte("CERTIFICATE"),
20+
Format: pb.Format_PEM,
21+
Key: []byte("KEY"),
22+
Name: "NAME",
23+
NamespaceId: "NAMESPACE_ID",
24+
OriginatorId: "terraform",
25+
}
26+
actual := provider.ConvertKeyPairToCreatePB(&provider.KeyPairModel{
27+
ID: types.StringValue("ID"),
28+
Name: types.StringValue("NAME"),
29+
NamespaceID: types.StringValue("NAMESPACE_ID"),
30+
Certificate: types.StringValue("CERTIFICATE"),
31+
Key: types.StringValue("KEY"),
32+
})
33+
if diff := cmp.Diff(expected, actual, protocmp.Transform()); diff != "" {
34+
t.Errorf("unexpected difference: %s", diff)
35+
}
36+
}
37+
38+
func TestConvertKeyPairToUpdatePB(t *testing.T) {
39+
t.Parallel()
40+
41+
fmt := pb.Format_PEM
42+
expected := &pb.UpdateKeyPairRequest{
43+
Certificate: []byte("CERTIFICATE"),
44+
Format: &fmt,
45+
Id: "ID",
46+
Key: []byte("KEY"),
47+
Name: proto.String("NAME"),
48+
OriginatorId: "terraform",
49+
}
50+
actual := provider.ConvertKeyPairToUpdatePB(&provider.KeyPairModel{
51+
ID: types.StringValue("ID"),
52+
Name: types.StringValue("NAME"),
53+
NamespaceID: types.StringValue("NAMESPACE_ID"),
54+
Certificate: types.StringValue("CERTIFICATE"),
55+
Key: types.StringValue("KEY"),
56+
})
57+
if diff := cmp.Diff(expected, actual, protocmp.Transform()); diff != "" {
58+
t.Errorf("unexpected difference: %s", diff)
59+
}
60+
}

internal/provider/models.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/pomerium/enterprise-client-go/pb"
1212
)
1313

14+
const originatorID = "terraform"
15+
1416
// ServiceAccountModel represents the shared model for service account resources and data sources
1517
type ServiceAccountModel struct {
1618
ID types.String `tfsdk:"id"`
@@ -77,8 +79,9 @@ func ConvertNamespaceToPB(_ context.Context, src *NamespaceResourceModel) (*pb.N
7779
var diagnostics diag.Diagnostics
7880

7981
pbNamespace := &pb.Namespace{
80-
Id: src.ID.ValueString(),
81-
Name: src.Name.ValueString(),
82+
OriginatorId: originatorID,
83+
Id: src.ID.ValueString(),
84+
Name: src.Name.ValueString(),
8285
}
8386

8487
if !src.ParentID.IsNull() {

internal/provider/policy_model.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,36 @@ import (
55

66
"github.com/hashicorp/terraform-plugin-framework/diag"
77
"github.com/hashicorp/terraform-plugin-framework/types"
8+
89
"github.com/pomerium/enterprise-client-go/pb"
910
)
1011

1112
// PolicyModel represents the shared model for policy resources and data sources
1213
type PolicyModel struct {
14+
Description types.String `tfsdk:"description"`
15+
Enforced types.Bool `tfsdk:"enforced"`
16+
Explanation types.String `tfsdk:"explanation"`
1317
ID types.String `tfsdk:"id"`
1418
Name types.String `tfsdk:"name"`
15-
Description types.String `tfsdk:"description"`
1619
NamespaceID types.String `tfsdk:"namespace_id"`
1720
PPL PolicyLanguage `tfsdk:"ppl"`
1821
Rego types.List `tfsdk:"rego"`
19-
Enforced types.Bool `tfsdk:"enforced"`
20-
Explanation types.String `tfsdk:"explanation"`
2122
Remediation types.String `tfsdk:"remediation"`
2223
}
2324

2425
func ConvertPolicyToPB(ctx context.Context, src *PolicyResourceModel) (*pb.Policy, diag.Diagnostics) {
2526
var diagnostics diag.Diagnostics
2627

2728
pbPolicy := &pb.Policy{
28-
Id: src.ID.ValueString(),
29-
Name: src.Name.ValueString(),
30-
Description: src.Description.ValueString(),
31-
NamespaceId: src.NamespaceID.ValueString(),
32-
Ppl: string(src.PPL.PolicyJSON),
33-
Enforced: src.Enforced.ValueBool(),
34-
Explanation: src.Explanation.ValueString(),
35-
Remediation: src.Remediation.ValueString(),
29+
OriginatorId: originatorID,
30+
Id: src.ID.ValueString(),
31+
Name: src.Name.ValueString(),
32+
Description: src.Description.ValueString(),
33+
NamespaceId: src.NamespaceID.ValueString(),
34+
Ppl: string(src.PPL.PolicyJSON),
35+
Enforced: src.Enforced.ValueBool(),
36+
Explanation: src.Explanation.ValueString(),
37+
Remediation: src.Remediation.ValueString(),
3638
}
3739
diagnostics.Append(src.Rego.ElementsAs(ctx, &pbPolicy.Rego, false)...)
3840

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package provider_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
8+
"github.com/hashicorp/terraform-plugin-framework/attr"
9+
"github.com/hashicorp/terraform-plugin-framework/types"
10+
"github.com/stretchr/testify/assert"
11+
"google.golang.org/protobuf/testing/protocmp"
12+
13+
"github.com/pomerium/enterprise-client-go/pb"
14+
"github.com/pomerium/enterprise-terraform-provider/internal/provider"
15+
)
16+
17+
func TestConvertPolicyToPB(t *testing.T) {
18+
t.Parallel()
19+
20+
expected := &pb.Policy{
21+
Description: "DESCRIPTION",
22+
Enforced: true,
23+
Explanation: "EXPLANATION",
24+
Id: "ID",
25+
Name: "NAME",
26+
NamespaceId: "NAMESPACE_ID",
27+
OriginatorId: "terraform",
28+
Rego: []string{"REGO"},
29+
Remediation: "REMEDIATION",
30+
}
31+
actual, diag := provider.ConvertPolicyToPB(context.Background(), &provider.PolicyModel{
32+
Description: types.StringValue("DESCRIPTION"),
33+
Enforced: types.BoolValue(true),
34+
Explanation: types.StringValue("EXPLANATION"),
35+
ID: types.StringValue("ID"),
36+
Name: types.StringValue("NAME"),
37+
NamespaceID: types.StringValue("NAMESPACE_ID"),
38+
PPL: provider.PolicyLanguage{},
39+
Rego: types.ListValueMust(types.StringType, []attr.Value{types.StringValue("REGO")}),
40+
Remediation: types.StringValue("REMEDIATION"),
41+
})
42+
if !assert.Equal(t, 0, diag.ErrorsCount()) {
43+
t.Log(diag.Errors())
44+
}
45+
if diff := cmp.Diff(expected, actual, protocmp.Transform()); diff != "" {
46+
t.Errorf("unexpected difference: %s", diff)
47+
}
48+
}

internal/provider/route_model.go

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ func ConvertRouteToPB(
181181
pbRoute.RewriteResponseHeaders = rewriteHeadersToPB(src.RewriteResponseHeaders)
182182
pbRoute.BearerTokenFormat = ToBearerTokenFormat(src.BearerTokenFormat)
183183
ToRouteStringList(ctx, &pbRoute.IdpAccessTokenAllowedAudiences, src.IDPAccessTokenAllowedAudiences, &diagnostics)
184+
pbRoute.OriginatorId = originatorID
184185

185186
return pbRoute, diagnostics
186187
}

internal/provider/route_model_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55
"testing"
66

77
"github.com/hashicorp/terraform-plugin-framework/types"
8-
"github.com/pomerium/enterprise-client-go/pb"
9-
"github.com/pomerium/enterprise-terraform-provider/internal/provider"
108
"github.com/stretchr/testify/assert"
119
"github.com/stretchr/testify/require"
10+
11+
"github.com/pomerium/enterprise-client-go/pb"
12+
"github.com/pomerium/enterprise-terraform-provider/internal/provider"
1213
)
1314

1415
func TestConvertRouteFromPB(t *testing.T) {

internal/provider/route_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ func TestConvertRoute(t *testing.T) {
176176
require.False(t, diag.HasError(), diag.Errors())
177177

178178
expected := &pb.Route{
179+
OriginatorId: "terraform",
179180
Id: "route-id",
180181
Name: "route-name",
181182
From: "from",

internal/provider/settings_model.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type SettingsModel struct {
6969
InsecureServer types.Bool `tfsdk:"insecure_server"`
7070
InstallationID types.String `tfsdk:"installation_id"`
7171
JWTClaimsHeaders types.Map `tfsdk:"jwt_claims_headers"`
72+
JWTGroupsFilter types.Object `tfsdk:"jwt_groups_filter"`
7273
LogLevel types.String `tfsdk:"log_level"`
7374
LogoURL types.String `tfsdk:"logo_url"`
7475
MetricsAddress types.String `tfsdk:"metrics_address"`
@@ -83,7 +84,6 @@ type SettingsModel struct {
8384
TimeoutIdle timetypes.GoDuration `tfsdk:"timeout_idle"`
8485
TimeoutRead timetypes.GoDuration `tfsdk:"timeout_read"`
8586
TimeoutWrite timetypes.GoDuration `tfsdk:"timeout_write"`
86-
JWTGroupsFilter types.Object `tfsdk:"jwt_groups_filter"`
8787
}
8888

8989
func ConvertSettingsToPB(
@@ -146,6 +146,7 @@ func ConvertSettingsToPB(
146146
pbSettings.LogLevel = src.LogLevel.ValueStringPointer()
147147
pbSettings.LogoUrl = src.LogoURL.ValueStringPointer()
148148
pbSettings.MetricsAddress = src.MetricsAddress.ValueStringPointer()
149+
pbSettings.OriginatorId = originatorID
149150
pbSettings.PassIdentityHeaders = src.PassIdentityHeaders.ValueBoolPointer()
150151
pbSettings.PrimaryColor = src.PrimaryColor.ValueStringPointer()
151152
pbSettings.ProxyLogLevel = src.ProxyLogLevel.ValueStringPointer()

0 commit comments

Comments
 (0)