Skip to content

Commit

Permalink
Empty policy = "", not "null"
Browse files Browse the repository at this point in the history
  • Loading branch information
Ole Markus With committed Mar 12, 2021
1 parent 2d465cc commit 04e0253
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/model/iam/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/model/iam/iam_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,9 @@ func (b *PolicyResource) Open() (io.Reader, error) {
if err != nil {
return nil, fmt.Errorf("error building IAM policy: %v", err)
}
if policy == nil {
return bytes.NewReader([]byte{}), nil
}
j, err := policy.AsJSON()
if err != nil {
return nil, fmt.Errorf("error building IAM policy: %v", err)
Expand Down
34 changes: 34 additions & 0 deletions pkg/model/iam/iam_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ import (

"github.com/aws/aws-sdk-go/aws"

"k8s.io/apimachinery/pkg/types"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/testutils"
"k8s.io/kops/pkg/testutils/golden"
"k8s.io/kops/pkg/util/stringorslice"
"k8s.io/kops/upup/pkg/fi"
)

func TestRoundTrip(t *testing.T) {
Expand Down Expand Up @@ -207,3 +210,34 @@ func TestPolicyGeneration(t *testing.T) {
golden.AssertMatchesFile(t, actualPolicy, x.Policy)
}
}

func TestEmptyPolicy(t *testing.T) {

role := &GenericServiceAccount{
NamespacedName: types.NamespacedName{
Name: "myaccount",
Namespace: "default",
},
Policy: nil,
}

cluster := testutils.BuildMinimalCluster("irsa.example.com")
b := &PolicyBuilder{
Cluster: cluster,
Role: role,
}

pr := &PolicyResource{
Builder: b,
}

policy, err := fi.ResourceAsString(pr)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if policy != "" {
t.Errorf("empty policy should result in empty string, but was %q", policy)
}

}

0 comments on commit 04e0253

Please sign in to comment.