-
Notifications
You must be signed in to change notification settings - Fork 106
CLOUDP-78181: project ip access list (part 1) #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b22ce9d
fix linter
d6f7742
Merge branch 'main' of github.com:mongodb/mongodb-atlas-kubernetes in…
b4c4637
Merge branch 'main' of github.com:mongodb/mongodb-atlas-kubernetes in…
dd2af24
Merge branch 'main' of github.com:mongodb/mongodb-atlas-kubernetes in…
9e20e7b
Merge branch 'main' of github.com:mongodb/mongodb-atlas-kubernetes in…
2c41036
Merge branch 'main' of github.com:mongodb/mongodb-atlas-kubernetes in…
6859d8a
Merge branch 'main' of github.com:mongodb/mongodb-atlas-kubernetes in…
ba8c974
Merge branch 'main' of github.com:mongodb/mongodb-atlas-kubernetes in…
9929dec
wip
a4d3a20
wip
21e7ab9
merged main
12ad0f0
merged main
7469406
remove F
2a8cedc
fix tests
ca378a9
fix test
38e5ff7
reasons formatting
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package atlasproject | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "time" | ||
|
|
||
| "go.mongodb.org/atlas/mongodbatlas" | ||
| "go.uber.org/zap" | ||
|
|
||
| mdbv1 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1" | ||
| "github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/atlas" | ||
| "github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/workflow" | ||
| "github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/set" | ||
| "github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/timeutil" | ||
| ) | ||
|
|
||
| // atlasProjectIPAccessList is a synonym of Atlas object as we need to implement 'Identifier' (and we cannot modify | ||
| // their object) | ||
| type atlasProjectIPAccessList mongodbatlas.ProjectIPAccessList | ||
|
|
||
| func (i atlasProjectIPAccessList) Identifier() interface{} { | ||
| return i.CIDRBlock + i.AwsSecurityGroup + i.IPAddress | ||
| } | ||
|
|
||
| func (r *AtlasProjectReconciler) ensureIPAccessList(ctx *workflow.Context, connection atlas.Connection, projectID string, project *mdbv1.AtlasProject) workflow.Result { | ||
| if err := validateIPAccessLists(project.Spec.ProjectIPAccessList); err != nil { | ||
| return workflow.Terminate(workflow.ProjectIPAccessInvalid, err.Error()) | ||
| } | ||
| active, _ := filterActiveIPAccessLists(project) | ||
|
|
||
| client, err := atlas.Client(r.AtlasDomain, connection, ctx.Log) | ||
| if err != nil { | ||
| return workflow.Terminate(workflow.Internal, err.Error()) | ||
| } | ||
| if result := createOrDeleteInAtlas(client, projectID, active, ctx.Log); !result.IsOk() { | ||
| return result | ||
| } | ||
| // TODO update status - add the expired project IP access list there | ||
| return workflow.OK() | ||
| } | ||
|
|
||
| func validateIPAccessLists(ipAccessList []mdbv1.ProjectIPAccessList) error { | ||
| for _, list := range ipAccessList { | ||
| if list.DeleteAfterDate != "" { | ||
| _, err := timeutil.ParseISO8601(list.DeleteAfterDate) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| // Go doesn't support XOR, but uses '!=' instead: https://stackoverflow.com/a/23025720/614239 | ||
| onlyOneSpecified := isNotEmpty(list.AwsSecurityGroup) != isNotEmpty(list.CIDRBlock) != isNotEmpty(list.IPAddress) | ||
vasilevp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if !onlyOneSpecified { | ||
| return errors.New("only one of the 'awsSecurityGroup', 'cidrBlock' or 'ipAddress' is required be specified") | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func createOrDeleteInAtlas(client *mongodbatlas.Client, projectID string, operatorIPAccessLists []mdbv1.ProjectIPAccessList, log *zap.SugaredLogger) workflow.Result { | ||
| atlasAccess, _, err := client.ProjectIPAccessList.List(context.Background(), projectID, &mongodbatlas.ListOptions{}) | ||
| if err != nil { | ||
| return workflow.Terminate(workflow.ProjectIPNotCreatedInAtlas, err.Error()) | ||
| } | ||
| // Making a new slice with synonyms as Atlas IP Access list to enable usage of 'Identifiable' | ||
| atlasAccessLists := make([]atlasProjectIPAccessList, len(atlasAccess.Results)) | ||
| for i, r := range atlasAccess.Results { | ||
| atlasAccessLists[i] = atlasProjectIPAccessList(r) | ||
| } | ||
|
|
||
| difference := set.Difference(atlasAccessLists, operatorIPAccessLists) | ||
|
|
||
| if err := deleteIPAccessFromAtlas(client, projectID, difference, log); err != nil { | ||
| return workflow.Terminate(workflow.ProjectIPNotCreatedInAtlas, err.Error()) | ||
| } | ||
|
|
||
| if result := createIPAccessListsInAtlas(client, projectID, operatorIPAccessLists); !result.IsOk() { | ||
| return result | ||
| } | ||
| return workflow.OK() | ||
| } | ||
|
|
||
| func createIPAccessListsInAtlas(client *mongodbatlas.Client, projectID string, ipAccessLists []mdbv1.ProjectIPAccessList) workflow.Result { | ||
| operatorAccessLists := make([]*mongodbatlas.ProjectIPAccessList, len(ipAccessLists)) | ||
| for i, list := range ipAccessLists { | ||
| atlasFormat, err := list.ToAtlas() | ||
| if err != nil { | ||
| return workflow.Terminate(workflow.Internal, err.Error()) | ||
| } | ||
| operatorAccessLists[i] = atlasFormat | ||
| } | ||
|
|
||
| if _, _, err := client.ProjectIPAccessList.Create(context.Background(), projectID, operatorAccessLists); err != nil { | ||
| return workflow.Terminate(workflow.ProjectIPNotCreatedInAtlas, err.Error()) | ||
| } | ||
| return workflow.OK() | ||
| } | ||
|
|
||
| func deleteIPAccessFromAtlas(client *mongodbatlas.Client, projectID string, listsToRemove []set.Identifiable, log *zap.SugaredLogger) error { | ||
| for _, l := range listsToRemove { | ||
| if _, err := client.ProjectIPAccessList.Delete(context.Background(), projectID, l.Identifier().(string)); err != nil { | ||
| return err | ||
| } | ||
| log.Debugw("Removed IPAccessList from Atlas as it's not specified in current AtlasProject", "id", l.Identifier()) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func filterActiveIPAccessLists(project *mdbv1.AtlasProject) ([]mdbv1.ProjectIPAccessList, []mdbv1.ProjectIPAccessList) { | ||
| active := make([]mdbv1.ProjectIPAccessList, 0) | ||
| expired := make([]mdbv1.ProjectIPAccessList, 0) | ||
| for _, list := range project.Spec.ProjectIPAccessList { | ||
| if list.DeleteAfterDate != "" { | ||
| // We are ignoring the error as it will never happen due to validation check before | ||
| iso8601, _ := timeutil.ParseISO8601(list.DeleteAfterDate) | ||
vasilevp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if iso8601.Before(time.Now()) { | ||
| expired = append(expired, list) | ||
| continue | ||
| } | ||
| } | ||
| // Either 'deleteAfterDate' field is not specified or it's higher than the current time | ||
| active = append(active, list) | ||
| } | ||
| return active, expired | ||
| } | ||
|
|
||
| func isNotEmpty(s string) bool { | ||
| return s != "" | ||
| } | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package timeutil | ||
|
|
||
| import "time" | ||
|
|
||
| func ParseISO8601(dateTime string) (time.Time, error) { | ||
| return time.Parse(dateTime, "2006-01-02T15:04:05-0700") | ||
vasilevp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] but
!=literally isXORfor booleans 🙂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes! :)