Skip to content

Commit 5e88755

Browse files
committed
ppl: handle unknown state
1 parent 53f0835 commit 5e88755

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

example/main.tf

+18-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
pomerium = {
44
source = "pomerium/pomerium"
5-
version = "0.0.2"
5+
version = "0.0.5"
66
}
77
}
88
}
@@ -50,22 +50,30 @@ resource "pomerium_settings" "settings" {
5050

5151
log_level = "info"
5252
proxy_log_level = "info"
53-
# tracing_provider = "jaeger"
54-
# tracing_sample_rate = 1
55-
# tracing_jaeger_collector_endpoint = "http://localhost:14268/api/traces"
56-
# tracing_jaeger_agent_endpoint = "localhost:6831"
5753

5854
timeout_idle = "5m"
5955
}
6056

57+
resource "pomerium_service_account" "test_sa" {
58+
namespace_id = pomerium_namespace.test_namespace.id
59+
name = "test-service-account"
60+
}
61+
6162
resource "pomerium_policy" "test_policy" {
63+
depends_on = [pomerium_service_account.test_sa]
6264
name = "test-policy"
6365
namespace_id = pomerium_namespace.test_namespace.id
64-
ppl = <<EOF
65-
- allow:
66-
and:
67-
- authenticated_user: true
68-
EOF
66+
ppl = yamlencode({
67+
allow = {
68+
and = [
69+
{
70+
user = {
71+
is = pomerium_service_account.test_sa.id
72+
}
73+
}
74+
]
75+
}
76+
})
6977
}
7078

7179
resource "pomerium_route" "test_route" {

internal/provider/policy_types.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework/diag"
1212
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1313
"github.com/hashicorp/terraform-plugin-go/tftypes"
14+
"github.com/hashicorp/terraform-plugin-log/tflog"
1415

1516
"github.com/pomerium/pomerium/pkg/policy/parser"
1617
)
@@ -42,7 +43,14 @@ func (p PolicyLanguageType) Equal(o attr.Type) bool {
4243

4344
func (PolicyLanguageType) Parse(src basetypes.StringValue) (PolicyLanguage, error) {
4445
if src.IsNull() {
45-
return PolicyLanguage{}, nil
46+
return PolicyLanguage{
47+
StringValue: basetypes.NewStringNull(),
48+
}, nil
49+
}
50+
if src.IsUnknown() {
51+
return PolicyLanguage{
52+
StringValue: basetypes.NewStringUnknown(),
53+
}, nil
4654
}
4755

4856
ppl, err := parser.New().ParseYAML(strings.NewReader(src.ValueString()))
@@ -61,13 +69,14 @@ func (PolicyLanguageType) Parse(src basetypes.StringValue) (PolicyLanguage, erro
6169
}
6270

6371
func (PolicyLanguageType) ValueFromString(
64-
_ context.Context,
72+
ctx context.Context,
6573
in basetypes.StringValue,
6674
) (basetypes.StringValuable, diag.Diagnostics) {
75+
tflog.Info(ctx, "PPL.ValueFromString", map[string]any{"in": in})
6776
var diag diag.Diagnostics
6877
v, err := PolicyLanguageType{}.Parse(in)
6978
if err != nil {
70-
diag.AddError("failed to parse PPL", err.Error()+">>"+in.ValueString()+"<<")
79+
diag.AddError("failed to parse PPL", err.Error())
7180
return nil, diag
7281
}
7382
return v, nil
@@ -77,6 +86,7 @@ func (p PolicyLanguageType) ValueFromTerraform(
7786
ctx context.Context,
7887
in tftypes.Value,
7988
) (attr.Value, error) {
89+
tflog.Info(ctx, "PPL.ValueFromTerraform", map[string]any{"in": in})
8090
attrValue, err := p.StringType.ValueFromTerraform(ctx, in)
8191
if err != nil {
8292
return nil, err

internal/provider/policy_types_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ func TestPolicyTypes(t *testing.T) {
3535
},
3636
"null": {
3737
in: tftypes.NewValue(tftypes.String, nil),
38-
expected: provider.PolicyLanguage{},
38+
expected: provider.PolicyLanguage{StringValue: basetypes.NewStringNull()},
39+
},
40+
"unknown": {
41+
in: tftypes.NewValue(tftypes.String, tftypes.UnknownValue),
42+
expected: provider.PolicyLanguage{StringValue: basetypes.NewStringUnknown()},
3943
},
4044
}
4145
for name, testCase := range testCases {

0 commit comments

Comments
 (0)