Skip to content

Commit b6390a7

Browse files
Fixed encryption at REST aws.roleID (#987)
Fixed encryption at rest aws.roleID. Added missing permissions for DataFederations
1 parent 4cab3dd commit b6390a7

12 files changed

+242
-10
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ e2e-openshift-upgrade:
107107

108108
.PHONY: manager
109109
manager: generate fmt vet ## Build manager binary
110-
@echo "Building operator with version $(VERSION)"
111-
GOOS=$(TARGET_OS) GOARCH=$(TARGET_ARCH) go build -o bin/manager -ldflags="-X github.com/mongodb/mongodb-atlas-kubernetes/pkg/version.Version=$(VERSION)" cmd/manager/main.go
110+
@echo "Building operator with version $(VERSION); $(TARGET_OS) - $(TARGET_ARCH)}"
111+
CGO_ENABLED=0 GOOS=$(TARGET_OS) GOARCH=$(TARGET_ARCH) go build -o bin/manager -ldflags="-X github.com/mongodb/mongodb-atlas-kubernetes/pkg/version.Version=$(VERSION)" cmd/manager/main.go
112112

113113
.PHONY: run
114114
run: generate fmt vet manifests ## Run against the configured Kubernetes cluster in ~/.kube/config

PROJECT

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,13 @@ resources:
6161
kind: AtlasTeam
6262
path: github.com/mongodb/mongodb-atlas-kubernetes/api/v1
6363
version: v1
64+
- api:
65+
crdVersion: v1
66+
namespaced: true
67+
controller: true
68+
domain: mongodb.com
69+
group: atlas
70+
kind: AtlasDataFederation
71+
path: github.com/mongodb/mongodb-atlas-kubernetes/api/v1
72+
version: v1
6473
version: "3"

config/crd/bases/atlas.mongodb.com_atlasdatafederations.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ spec:
1515
singular: atlasdatafederation
1616
scope: Namespaced
1717
versions:
18-
- name: v1
18+
- additionalPrinterColumns:
19+
- jsonPath: .spec.name
20+
name: Name
21+
type: string
22+
name: v1
1923
schema:
2024
openAPIV3Schema:
25+
description: AtlasDataFederation is the Schema for the Atlas Data Federation
26+
API
2127
properties:
2228
apiVersion:
2329
description: 'APIVersion defines the versioned schema of this representation
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# The following patch adds a directive for certmanager to inject CA into the CRD
2+
# CRD conversion requires k8s 1.13 or later.
3+
apiVersion: apiextensions.k8s.io/v1beta1
4+
kind: CustomResourceDefinition
5+
metadata:
6+
annotations:
7+
cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME)
8+
name: atlasdatafederations.atlas.mongodb.com
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# The following patch enables conversion webhook for CRD
2+
# CRD conversion requires k8s 1.13 or later.
3+
apiVersion: apiextensions.k8s.io/v1beta1
4+
kind: CustomResourceDefinition
5+
metadata:
6+
name: atlasdatafederations.atlas.mongodb.com
7+
spec:
8+
conversion:
9+
strategy: Webhook
10+
webhookClientConfig:
11+
# this is "\n" used as a placeholder, otherwise it will be rejected by the apiserver for being blank,
12+
# but we're going to set it later using the cert-manager (or potentially a patch if not using cert-manager)
13+
caBundle: Cg==
14+
service:
15+
namespace: system
16+
name: webhook-service
17+
path: /convert
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apiVersion: atlas.mongodb.com/v1
2+
kind: AtlasDataFederation
3+
metadata:
4+
name: my-data-federation
5+
namespace: mongodb-atlas-system
6+
spec:
7+
projectRef:
8+
name: my-project
9+
namespace: mongodb-atlas-system
10+
name: my-data-federation
11+
privateEndpoints:
12+
- endpointId: vpce-03f9eeaa764e32454
13+
provider: AWS
14+
type: DATA_LAKE
15+
storage:
16+
stores:
17+
- name: http-test
18+
provider: http
19+
databases:
20+
- name: test-db-1
21+
collections:
22+
- name: test-collection-1
23+
dataSources:
24+
- storeName: http-test
25+
urls:
26+
- https://data.cityofnewyork.us/api/views/vfnx-vebw/rows.csv
27+
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2020 MongoDB.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package v1
218

319
import (
@@ -115,10 +131,7 @@ func (pe DataFederationPE) Identifier() interface{} {
115131
// +kubebuilder:subresource:status
116132
// +groupName:=atlas.mongodb.com
117133

118-
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
119-
// +kubebuilder:object:root=true
120-
// +kubebuilder:subresource:status
121-
134+
// AtlasDataFederation is the Schema for the Atlas Data Federation API
122135
type AtlasDataFederation struct {
123136
metav1.TypeMeta `json:",inline"`
124137
metav1.ObjectMeta `json:"metadata,omitempty"`
File renamed without changes.

pkg/controller/atlasproject/encryption_at_rest.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package atlasproject
22

33
import (
44
"context"
5+
"fmt"
56
"reflect"
7+
"regexp"
68

79
mdbv1 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1"
810
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/status"
@@ -12,6 +14,10 @@ import (
1214
"go.mongodb.org/atlas/mongodbatlas"
1315
)
1416

17+
const (
18+
ObjectIDRegex = "^([a-f0-9]{24})$"
19+
)
20+
1521
func ensureEncryptionAtRest(ctx *workflow.Context, projectID string, project *mdbv1.AtlasProject) workflow.Result {
1622
result := createOrDeleteEncryptionAtRests(ctx, projectID, project)
1723
if !result.IsOk() {
@@ -67,13 +73,45 @@ func syncEncryptionAtRestsInAtlas(ctx *workflow.Context, projectID string, proje
6773
GoogleCloudKms: getGoogleCloudKms(project),
6874
}
6975

76+
if err := normalizeAwsKms(ctx, projectID, &requestBody.AwsKms); err != nil {
77+
return err
78+
}
79+
7080
if _, _, err := ctx.Client.EncryptionsAtRest.Create(context.Background(), &requestBody); err != nil { // Create() sends PATCH request
7181
return err
7282
}
7383

7484
return nil
7585
}
7686

87+
func normalizeAwsKms(ctx *workflow.Context, projectID string, awsKms *mongodbatlas.AwsKms) error {
88+
// verify if role ID is set as AtlasObjectID
89+
matched, err := regexp.MatchString(ObjectIDRegex, awsKms.RoleID)
90+
if err != nil {
91+
ctx.Log.Debugf("normalizing aws kms roleID failed: %v", err)
92+
return err
93+
}
94+
if matched {
95+
return nil
96+
}
97+
98+
// assume that role ID is set as AWS ARN
99+
resp, _, err := ctx.Client.CloudProviderAccess.ListRoles(context.Background(), projectID)
100+
if err != nil {
101+
return err
102+
}
103+
104+
for _, role := range resp.AWSIAMRoles {
105+
if role.IAMAssumedRoleARN == awsKms.RoleID {
106+
awsKms.RoleID = role.RoleID
107+
return nil
108+
}
109+
}
110+
111+
ctx.Log.Debugf("no match for provided AWS RoleID ARN: '%s'. Is the CPA configured for the project?", awsKms.RoleID)
112+
return fmt.Errorf("can not use '%s' aws roleID for encryption at rest. AWS ARN not configured as Cloud Provider Access", awsKms.RoleID)
113+
}
114+
77115
func AtlasInSync(atlas *mongodbatlas.EncryptionAtRest, spec *mdbv1.EncryptionAtRest) (bool, error) {
78116
if IsEncryptionAtlasEmpty(atlas) && IsEncryptionSpecEmpty(spec) {
79117
return true, nil

0 commit comments

Comments
 (0)