-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLOUDP-167207: Validate GCP Service Account Key
Signed-off-by: Jose Vazquez <[email protected]>
- Loading branch information
Showing
2 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package validate | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/status" | ||
|
@@ -441,3 +442,99 @@ func TestBackupScheduleValidation(t *testing.T) { | |
}) | ||
}) | ||
} | ||
|
||
// this key was removed immediately after download, so don't bother | ||
const sampleSAKey = `{ | ||
"type": "service_account", | ||
"project_id": "some-project", | ||
"private_key_id": "dc6c401f0acd0147ca70e3169f579b570583b58f", | ||
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCviL4+Bnn759sV\nNrtyexwHtR/5JYzwivupqOMdz1zuscqKfJOo6RXzq7Em3saoxLpHwwJzPq+HX1D+\ndaE0fB2hO0Mfjkcgmro0jBbLnRJgFCx7NwkyDfj8z6i4zx2CxZLiwdqo3Q3hEMNy\n5oZs52tFlTkOALCxa76Aq4eUIblupyhlhETQB1fb4D9+U57b4eeeFRyccwR7Cg0x\nfZUE1udV7YwKphLS8dCbLoqAQ3jmaxv+Qjo8e5Mj5oaMxzRAdOO1VtG9GaLU96Rc\nKGS75w+E/eR3Pm7b2dFo3jba2jvgm3U++EJi5/0zL/TQnOMwNHASPUsYQAhZezB5\nh5/MDwmjAgMBAAECggEAIOccZer33Zip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ASm9BXSN8Ru+dP5/E55mb\nd//aQlxlIvROhWSnotGzhyQ6DVk2fRRZQAuTEFVEprBX87gckdzb5cdaomziO9be\nhmtv1ValgmOCnta2AYw1blvfGK7B5FEpFckMniLjWap08aironIImj6ligLWDqc0\nNbdyAvc6N/5qG8gu4f8C2Q4=\n-----END PRIVATE KEY-----\n", | ||
"client_email": "[email protected]", | ||
"client_id": "117865750705662546099", | ||
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | ||
"token_uri": "https://oauth2.googleapis.com/token", | ||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | ||
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/619108922856-compute%40developer.gserviceaccount.com", | ||
"universe_domain": "googleapis.com" | ||
}` | ||
|
||
const sampleSAKeyOneLine = `{ "type": "service_account", "project_id": "some-project", "private_key_id": "dc6c401f0acd0147ca70e3169f579b570583b58f", "private_key": "-----BEGIN PRIVATE KEY-----\\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCviL4+Bnn759sV\\nNrtyexwHtR/5JYzwivupqOMdz1zuscqKfJOo6RXzq7Em3saoxLpHwwJzPq+HX1D+\\ndaE0fB2hO0Mfjkcgmro0jBbLnRJgFCx7NwkyDfj8z6i4zx2CxZLiwdqo3Q3hEMNy\\n5oZs52tFlTkOALCxa76Aq4eUIblupyhlhETQB1fb4D9+U57b4eeeFRyccwR7Cg0x\\nfZUE1udV7YwKphLS8dCbLoqAQ3jmaxv+Qjo8e5Mj5oaMxzRAdOO1VtG9GaLU96Rc\\nKGS75w+E/eR3Pm7b2dFo3jba2jvgm3U++EJi5/0zL/TQnOMwNHASPUsYQAhZezB5\\nh5/MDwmjAgMBAAECggEAIOccZer33Zipz9GpFD3wVJ+GZUC9KO+cWcJ/A/z1Ggb4\\nhLnyQbSjOUAjHjqe+U6a7k2m/WwwIctjlrb85yYmtayymc0lFv75zVS/Bx6jrZ/K\\ncLQxxJCq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ta2AYw1blvfGK7B5FEpFckMniLjWap08aironIImj6ligLWDqc0\\nNbdyAvc6N/5qG8gu4f8C2Q4=\\n-----END PRIVATE KEY-----\\n", "client_email": "[email protected]", "client_id": "117865750705662546099", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/619108922856-compute%40developer.gserviceaccount.com", "universe_domain": "googleapis.com"}` | ||
|
||
func projectWithEncryptionAtRest(enabled bool) *mdbv1.AtlasProject { | ||
flag := enabled | ||
return &mdbv1.AtlasProject{ | ||
Spec: mdbv1.AtlasProjectSpec{ | ||
EncryptionAtRest: &mdbv1.EncryptionAtRest{ | ||
GoogleCloudKms: mdbv1.GoogleCloudKms{ | ||
Enabled: &flag, | ||
ServiceAccountKey: sampleSAKey, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func withProperUrls(properties string) string { | ||
urls := `"auth_uri": "https://accounts.google.com/o/oauth2/auth", | ||
"token_uri": "https://oauth2.googleapis.com/token", | ||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | ||
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/619108922856-compute%40developer.gserviceaccount.com"` | ||
return fmt.Sprintf(`{%s, %s}`, urls, properties) | ||
} | ||
|
||
func TestEncryptionAtRestValidation(t *testing.T) { | ||
t.Run("google service account key validation succeeds if no encryption at rest is used", func(t *testing.T) { | ||
prj := &mdbv1.AtlasProject{Spec: mdbv1.AtlasProjectSpec{}} | ||
assert.NoError(t, encryptionAtRest(prj)) | ||
}) | ||
|
||
t.Run("google service account key validation succeeds if encryption at rest is disabled", func(t *testing.T) { | ||
assert.NoError(t, encryptionAtRest(projectWithEncryptionAtRest(false))) | ||
}) | ||
|
||
t.Run("google service account key validation succeeds if encryption is enabled but the key is empty", func(t *testing.T) { | ||
assert.NoError(t, encryptionAtRest(projectWithEncryptionAtRest(true))) | ||
}) | ||
|
||
t.Run("google service account key validation succeeds for a good key", func(t *testing.T) { | ||
prj := projectWithEncryptionAtRest(true) | ||
prj.Spec.EncryptionAtRest.GoogleCloudKms.ServiceAccountKey = sampleSAKey | ||
assert.NoError(t, encryptionAtRest(prj)) | ||
}) | ||
|
||
t.Run("google service account key validation succeeds for a good key in a single line", func(t *testing.T) { | ||
prj := projectWithEncryptionAtRest(true) | ||
prj.Spec.EncryptionAtRest.GoogleCloudKms.ServiceAccountKey = sampleSAKeyOneLine | ||
assert.NoError(t, encryptionAtRest(prj)) | ||
}) | ||
|
||
t.Run("google service account key validation fails for an empty json key", func(t *testing.T) { | ||
prj := projectWithEncryptionAtRest(true) | ||
prj.Spec.EncryptionAtRest.GoogleCloudKms.ServiceAccountKey = "{}" | ||
assert.ErrorContains(t, encryptionAtRest(prj), "invalid empty service account key") | ||
}) | ||
|
||
t.Run("google service account key validation fails for an empty array json as key", func(t *testing.T) { | ||
prj := projectWithEncryptionAtRest(true) | ||
prj.Spec.EncryptionAtRest.GoogleCloudKms.ServiceAccountKey = "[]" | ||
assert.ErrorContains(t, encryptionAtRest(prj), "cannot unmarshal array into Go value") | ||
}) | ||
|
||
t.Run("google service account key validation fails for a json object with a wrong field type", func(t *testing.T) { | ||
prj := projectWithEncryptionAtRest(true) | ||
prj.Spec.EncryptionAtRest.GoogleCloudKms.ServiceAccountKey = `{"type":true}` | ||
assert.ErrorContains(t, encryptionAtRest(prj), "cannot unmarshal bool") | ||
}) | ||
|
||
t.Run("google service account key validation fails for a bad pem key", func(t *testing.T) { | ||
prj := projectWithEncryptionAtRest(true) | ||
prj.Spec.EncryptionAtRest.GoogleCloudKms.ServiceAccountKey = withProperUrls(`"private_key":"-----BEGIN PRIVATE KEY-----\nMIIEvQblah\n-----END PRIVATE KEY-----\n"`) | ||
assert.ErrorContains(t, encryptionAtRest(prj), "failed to decode PEM") | ||
}) | ||
|
||
t.Run("google service account key validation fails for a bad URL", func(t *testing.T) { | ||
prj := projectWithEncryptionAtRest(true) | ||
prj.Spec.EncryptionAtRest.GoogleCloudKms.ServiceAccountKey = withProperUrls(`"token_uri": "http//badurl.example"`) | ||
assert.ErrorContains(t, encryptionAtRest(prj), "invalid URL address") | ||
}) | ||
} |