|
@@ -1054,6 +1054,54 @@ func TestSetSecret(t *testing.T) {
|
|
|
err: nil,
|
|
err: nil,
|
|
|
},
|
|
},
|
|
|
},
|
|
},
|
|
|
|
|
+ "SetSecretWithEmptyExistingResourcePolicy": {
|
|
|
|
|
+ reason: "sync a resource policy when no existing policy is present",
|
|
|
|
|
+ args: args{
|
|
|
|
|
+ store: makeValidSecretStore().Spec.Provider.AWS,
|
|
|
|
|
+ client: fakesm.Client{
|
|
|
|
|
+ GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
|
|
|
|
|
+ PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
|
|
|
|
|
+ DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
|
|
|
|
|
+ TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
|
|
|
|
|
+ UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
|
|
|
|
|
+ GetResourcePolicyFn: fakesm.NewGetResourcePolicyFn(&awssm.GetResourcePolicyOutput{}, nil),
|
|
|
|
|
+ PutResourcePolicyFn: fakesm.NewPutResourcePolicyFn(&awssm.PutResourcePolicyOutput{}, nil),
|
|
|
|
|
+ },
|
|
|
|
|
+ pushSecretData: fake.PushSecretData{
|
|
|
|
|
+ SecretKey: secretKey,
|
|
|
|
|
+ RemoteKey: fakeKey,
|
|
|
|
|
+ Property: "",
|
|
|
|
|
+ Metadata: &apiextensionsv1.JSON{
|
|
|
|
|
+ Raw: []byte(`{
|
|
|
|
|
+ "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
|
|
|
|
|
+ "kind": "PushSecretMetadata",
|
|
|
|
|
+ "spec": {
|
|
|
|
|
+ "secretPushFormat": "string",
|
|
|
|
|
+ "resourcePolicy": {
|
|
|
|
|
+ "blockPublicPolicy": true,
|
|
|
|
|
+ "policySourceRef": {
|
|
|
|
|
+ "kind": "ConfigMap",
|
|
|
|
|
+ "name": "resource-policy",
|
|
|
|
|
+ "key": "policy.json"
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }`),
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ kubeclient: clientfake.NewFakeClient(&corev1.ConfigMap{
|
|
|
|
|
+ ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
+ Name: "resource-policy",
|
|
|
|
|
+ },
|
|
|
|
|
+ Data: map[string]string{
|
|
|
|
|
+ "policy.json": `{"Version":"2012-10-17","Statement":[{"Sid":"DenyAll","Effect":"Deny","Principal":"*","Action":"secretsmanager:GetSecretValue","Resource":"*"}]}`,
|
|
|
|
|
+ },
|
|
|
|
|
+ }),
|
|
|
|
|
+ },
|
|
|
|
|
+ want: want{
|
|
|
|
|
+ err: nil,
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
"SetSecretWithExistingNonChangingResourcePolicy": {
|
|
"SetSecretWithExistingNonChangingResourcePolicy": {
|
|
|
reason: "sync an existing secret without syncing resource policy that has no change",
|
|
reason: "sync an existing secret without syncing resource policy that has no change",
|
|
|
args: args{
|
|
args: args{
|
|
@@ -1397,6 +1445,89 @@ func TestPushSecretResourcePolicyUpdatedWhenValueUnchanged(t *testing.T) {
|
|
|
assert.JSONEq(t, newPolicy, *capturedPolicyInput.ResourcePolicy)
|
|
assert.JSONEq(t, newPolicy, *capturedPolicyInput.ResourcePolicy)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func TestPushSecretEmptyExistingResourcePolicy(t *testing.T) {
|
|
|
|
|
+ secretKey := fakeSecretKey
|
|
|
|
|
+ secretValue := []byte("fake-value")
|
|
|
|
|
+ fakeSecret := &corev1.Secret{
|
|
|
|
|
+ Data: map[string][]byte{
|
|
|
|
|
+ secretKey: secretValue,
|
|
|
|
|
+ },
|
|
|
|
|
+ }
|
|
|
|
|
+ arn := testARN
|
|
|
|
|
+ defaultVersion := testDefaultVersion
|
|
|
|
|
+ managed := managedBy
|
|
|
|
|
+ manager := externalSecrets
|
|
|
|
|
+
|
|
|
|
|
+ putResourcePolicyCalled := false
|
|
|
|
|
+
|
|
|
|
|
+ newPolicy := `{"Version":"2012-10-17","Statement":[{"Sid":"DenyAll","Effect":"Deny","Principal":"*","Action":"secretsmanager:GetSecretValue","Resource":"*"}]}`
|
|
|
|
|
+
|
|
|
|
|
+ client := fakesm.Client{
|
|
|
|
|
+ GetSecretValueFn: fakesm.NewGetSecretValueFn(&awssm.GetSecretValueOutput{
|
|
|
|
|
+ ARN: &arn,
|
|
|
|
|
+ SecretBinary: secretValue,
|
|
|
|
|
+ VersionId: &defaultVersion,
|
|
|
|
|
+ }, nil),
|
|
|
|
|
+ DescribeSecretFn: fakesm.NewDescribeSecretFn(&awssm.DescribeSecretOutput{
|
|
|
|
|
+ ARN: &arn,
|
|
|
|
|
+ Tags: []types.Tag{
|
|
|
|
|
+ {Key: &managed, Value: &manager},
|
|
|
|
|
+ },
|
|
|
|
|
+ VersionIdsToStages: map[string][]string{
|
|
|
|
|
+ defaultVersion: {"AWSCURRENT"},
|
|
|
|
|
+ },
|
|
|
|
|
+ }, nil),
|
|
|
|
|
+ PutSecretValueFn: fakesm.NewPutSecretValueFn(&awssm.PutSecretValueOutput{}, nil),
|
|
|
|
|
+ TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
|
|
|
|
|
+ UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
|
|
|
|
|
+ GetResourcePolicyFn: fakesm.NewGetResourcePolicyFn(&awssm.GetResourcePolicyOutput{}, nil),
|
|
|
|
|
+ PutResourcePolicyFn: fakesm.NewPutResourcePolicyFn(&awssm.PutResourcePolicyOutput{}, nil, func(input *awssm.PutResourcePolicyInput) {
|
|
|
|
|
+ putResourcePolicyCalled = true
|
|
|
|
|
+ }),
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ kubeclient := clientfake.NewFakeClient(&corev1.ConfigMap{
|
|
|
|
|
+ ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
+ Name: "resource-policy",
|
|
|
|
|
+ },
|
|
|
|
|
+ Data: map[string]string{
|
|
|
|
|
+ "policy.json": newPolicy,
|
|
|
|
|
+ },
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ sm := SecretsManager{
|
|
|
|
|
+ client: &client,
|
|
|
|
|
+ kube: kubeclient,
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ pushSecretData := fake.PushSecretData{
|
|
|
|
|
+ SecretKey: secretKey,
|
|
|
|
|
+ RemoteKey: fakeKey,
|
|
|
|
|
+ Property: "",
|
|
|
|
|
+ Metadata: &apiextensionsv1.JSON{
|
|
|
|
|
+ Raw: []byte(`{
|
|
|
|
|
+ "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
|
|
|
|
|
+ "kind": "PushSecretMetadata",
|
|
|
|
|
+ "spec": {
|
|
|
|
|
+ "secretPushFormat": "string",
|
|
|
|
|
+ "resourcePolicy": {
|
|
|
|
|
+ "blockPublicPolicy": true,
|
|
|
|
|
+ "policySourceRef": {
|
|
|
|
|
+ "kind": "ConfigMap",
|
|
|
|
|
+ "name": "resource-policy",
|
|
|
|
|
+ "key": "policy.json"
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }`),
|
|
|
|
|
+ },
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ err := sm.PushSecret(context.Background(), fakeSecret, pushSecretData)
|
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
+ assert.True(t, putResourcePolicyCalled, "PutResourcePolicy should be called when existing policy is empty")
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func TestDeleteSecret(t *testing.T) {
|
|
func TestDeleteSecret(t *testing.T) {
|
|
|
fakeClient := fakesm.Client{}
|
|
fakeClient := fakesm.Client{}
|
|
|
managed := managedBy
|
|
managed := managedBy
|