forked from spinnaker/clouddriver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request spinnaker#8 in SPKR/clouddriver-nflx from restrict…
…-iam-role-tagging to master * commit '70dae3ffca5a416dfe28d9edeb201d152ef11a1e': Support for restricting the set of users that can tag an iam role
- Loading branch information
Showing
6 changed files
with
200 additions
and
2 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
.../groovy/com/netflix/spinnaker/clouddriver/internal/RestrictIamRoleTaggingValidator.groovy
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2016 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
package com.netflix.spinnaker.clouddriver.internal | ||
|
||
import com.netflix.spinnaker.clouddriver.elasticsearch.descriptions.UpsertEntityTagsDescription | ||
import com.netflix.spinnaker.clouddriver.security.AllowedAccountsValidator | ||
import com.netflix.spinnaker.config.NetflixConfigurationProperties.OperationRestrictions | ||
import org.springframework.validation.Errors | ||
|
||
class RestrictIamRoleTaggingValidator implements AllowedAccountsValidator { | ||
private final OperationRestrictions restrictedOperations | ||
|
||
RestrictIamRoleTaggingValidator(OperationRestrictions restrictedOperations) { | ||
this.restrictedOperations = restrictedOperations | ||
} | ||
|
||
@Override | ||
void validate(String user, Collection<String> allowedAccounts, Object description, Errors errors) { | ||
if (!(description instanceof UpsertEntityTagsDescription)) { | ||
return | ||
} | ||
|
||
def entityType = description.entityRef?.entityType?.toLowerCase() | ||
def isAllowed = restrictedOperations.isAllowed("upsertEntityTags__${entityType}" as String, user) | ||
if (!isAllowed) { | ||
errors.rejectValue("credentials", "unauthorized", "${user} is not authorized to tag iam roles" as String) | ||
} | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
...r-nflx/src/main/groovy/com/netflix/spinnaker/config/NetflixConfigurationProperties.groovy
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2016 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
package com.netflix.spinnaker.config | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
|
||
@ConfigurationProperties(prefix = "netflix") | ||
class NetflixConfigurationProperties { | ||
OperationRestrictions operationRestrictions = [:] | ||
|
||
static class OperationRestrictions extends HashMap<String, List<String>> { | ||
boolean isAllowed(String operationType, String emailAddress) { | ||
def operationRestrictions = get(operationType) | ||
if (!operationRestrictions) { | ||
// attempt a case-insensitive lookup if exact match is not found | ||
operationType = keySet().find { it.equalsIgnoreCase(operationType) } | ||
operationRestrictions = operationType ? get(operationType) : null | ||
} | ||
|
||
// if there are no explicit operation restrictions, access should be granted by default | ||
return !operationRestrictions ? true : operationRestrictions.find { it.equalsIgnoreCase(emailAddress) } | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...ovy/com/netflix/spinnaker/clouddriver/internal/RestrictIamRoleTaggingValidatorSpec.groovy
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2016 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
package com.netflix.spinnaker.clouddriver.internal | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.netflix.spinnaker.clouddriver.elasticsearch.descriptions.UpsertEntityTagsDescription | ||
import com.netflix.spinnaker.config.NetflixConfigurationProperties | ||
import org.springframework.validation.Errors | ||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
class RestrictIamRoleTaggingValidatorSpec extends Specification { | ||
def errors = Mock(Errors) | ||
def objectMapper = new ObjectMapper() | ||
|
||
@Unroll | ||
def "should raise exception if authenticated user is not in set of allowed iam role taggers"() { | ||
given: | ||
def operationRestrictions = new NetflixConfigurationProperties.OperationRestrictions([ | ||
"upsertEntityTags__iamrole": ["test@test.com"] | ||
]) | ||
def preProcessor = new RestrictIamRoleTaggingValidator(operationRestrictions) | ||
|
||
when: | ||
preProcessor.validate(user, [], objectMapper.convertValue(description, UpsertEntityTagsDescription), errors) | ||
|
||
then: | ||
errorsCount * errors.rejectValue("credentials", "unauthorized", _) | ||
|
||
where: | ||
user | description || errorsCount | ||
null | [:] || 0 | ||
"test@test.com" | [entityRef: [entityType: "iamrole"]] || 0 | ||
null | [entityRef: [entityType: "servergroup"]] || 0 | ||
null | [entityRef: [entityType: "iamrole"]] || 1 | ||
"me@test.com" | [entityRef: [entityType: "iamrole"]] || 1 | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...lx/src/test/groovy/com/netflix/spinnaker/config/NetflixConfigurationPropertiesSpec.groovy
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2016 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
package com.netflix.spinnaker.config | ||
|
||
import spock.lang.Specification | ||
|
||
class NetflixConfigurationPropertiesSpec extends Specification { | ||
def "should check against configured operation restrictions"() { | ||
given: | ||
def configurationProperties = new NetflixConfigurationProperties( | ||
operationRestrictions: new NetflixConfigurationProperties.OperationRestrictions([ | ||
"upsertEntityTags__iamrole": ["test@test.com"] | ||
]) | ||
) | ||
|
||
expect: | ||
configurationProperties.operationRestrictions.isAllowed("upsertEntityTags", "me@test.com") | ||
configurationProperties.operationRestrictions.isAllowed("upsertEntityTags__iamrole", "test@test.com") | ||
configurationProperties.operationRestrictions.isAllowed("upsertEntityTags__iamrole", "TEST@TEST.COM") // case-insensitive | ||
!configurationProperties.operationRestrictions.isAllowed("upsertEntityTags__iamrole", "me@test.com") | ||
!configurationProperties.operationRestrictions.isAllowed("upsertentitytags__iamrole", "me@test.com") // case-insensitive | ||
} | ||
} |
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