forked from spinnaker/orca
-
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.
feat(echo): Support for restricting who can perform a manual judgment
This PR verifies the current user roles against `context.selectedStageRoles`. The operation is _only_ allowed if there is at least one overlapping role between `context.selectedStageRoles` and the current user roles. `context.selectedStageRoles` maps to what is currently being specified in the UI. This is a rewrite of what originally went in as spinnaker#3988.
- Loading branch information
Showing
6 changed files
with
340 additions
and
30 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
172 changes: 172 additions & 0 deletions
172
...-echo/src/main/java/com/netflix/spinnaker/orca/echo/util/ManualJudgmentAuthorization.java
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,172 @@ | ||
/* | ||
* Copyright 2020 OpsMx, 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.orca.echo.util; | ||
|
||
import static java.lang.String.format; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.base.Strings; | ||
import com.google.common.collect.Sets; | ||
import com.netflix.spinnaker.fiat.model.UserPermission; | ||
import com.netflix.spinnaker.fiat.model.resources.Role; | ||
import com.netflix.spinnaker.fiat.shared.FiatPermissionEvaluator; | ||
import com.netflix.spinnaker.fiat.shared.FiatStatus; | ||
import com.netflix.spinnaker.kork.exceptions.SpinnakerException; | ||
import com.netflix.spinnaker.orca.front50.Front50Service; | ||
import com.netflix.spinnaker.orca.front50.model.Application; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Component; | ||
import retrofit.RetrofitError; | ||
|
||
@Component | ||
public class ManualJudgmentAuthorization { | ||
private final Logger log = LoggerFactory.getLogger(getClass()); | ||
|
||
private final Front50Service front50Service; | ||
private final FiatPermissionEvaluator fiatPermissionEvaluator; | ||
|
||
private final FiatStatus fiatStatus; | ||
private final ObjectMapper objectMapper; | ||
|
||
@Autowired | ||
public ManualJudgmentAuthorization( | ||
Optional<Front50Service> front50Service, | ||
Optional<FiatPermissionEvaluator> fiatPermissionEvaluator, | ||
FiatStatus fiatStatus, | ||
ObjectMapper objectMapper) { | ||
this.front50Service = front50Service.orElse(null); | ||
this.fiatPermissionEvaluator = fiatPermissionEvaluator.orElse(null); | ||
|
||
this.fiatStatus = fiatStatus; | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
/** | ||
* A manual judgment will be considered "authorized" if the current user has at least one of the | ||
* required judgment roles _and_ read/write/execute permissions on the application being judged. | ||
* | ||
* @param application Application being judged | ||
* @param requiredJudgmentRoles Required judgment roles | ||
* @param currentUser User that has attempted this judgment | ||
* @return whether or not {@param currentUser} has authorization to judge | ||
*/ | ||
public boolean isAuthorized( | ||
String application, Collection<String> requiredJudgmentRoles, String currentUser) { | ||
if (!fiatStatus.isEnabled() || requiredJudgmentRoles.isEmpty()) { | ||
return true; | ||
} | ||
|
||
if (Strings.isNullOrEmpty(currentUser)) { | ||
return false; | ||
} | ||
|
||
UserPermission.View permission = fiatPermissionEvaluator.getPermission(currentUser); | ||
if (permission == null) { // Should never happen? | ||
log.warn("Attempted to get user permission for '{}' but none were found.", currentUser); | ||
return false; | ||
} | ||
|
||
return isAuthorized( | ||
requiredJudgmentRoles, | ||
permission.getRoles().stream().map(Role.View::getName).collect(Collectors.toList()), | ||
getApplicationPermissions(application)); | ||
} | ||
|
||
private boolean isAuthorized( | ||
Collection<String> requiredJudgmentRoles, | ||
Collection<String> currentUserRoles, | ||
Map<String, Object> applicationPermissions) { | ||
if (requiredJudgmentRoles == null || requiredJudgmentRoles.isEmpty()) { | ||
return true; | ||
} | ||
|
||
return Sets.intersection(new HashSet<>(requiredJudgmentRoles), new HashSet<>(currentUserRoles)) | ||
.size() | ||
> 0; | ||
|
||
// boolean isAuthorizedGroup = false; | ||
// | ||
// for (String role : currentUserRoles) { | ||
// if (requiredJudgmentRoles.contains(role)) { | ||
// for (Map.Entry<String, Object> entry : | ||
// applicationPermissions.entrySet()) { // get the application permission roles. | ||
// if (Authorization.CREATE.name().equals(entry.getKey()) | ||
// || Authorization.EXECUTE.name().equals(entry.getKey()) | ||
// || Authorization.WRITE.name().equals(entry.getKey())) { | ||
// // If the application permission roles has 'CREATE, EXECUTE, WRITE', then user is | ||
// // authorized. | ||
// if (entry.getValue() != null && ((List<String>) entry.getValue()).contains(role)) | ||
// { | ||
// return true; | ||
// } | ||
// } else if (Authorization.READ.name().equals(entry.getKey())) { | ||
// // If the application permission roles has 'READ', then user is not authorized. | ||
// if (entry.getValue() != null && ((List<String>) entry.getValue()).contains(role)) | ||
// { | ||
// isAuthorizedGroup = false; | ||
// } | ||
// } | ||
// } | ||
// } | ||
// } | ||
// | ||
// return isAuthorizedGroup; | ||
} | ||
|
||
private Map<String, Object> getApplicationPermissions(String applicationName) { | ||
return getApplication(applicationName) | ||
.map( | ||
application -> { | ||
if (application.getPermission().getPermissions() != null) { | ||
return objectMapper.convertValue( | ||
application.getPermission().getPermissions(), | ||
new TypeReference<Map<String, Object>>() {}); | ||
} | ||
|
||
return new HashMap<String, Object>(); | ||
}) | ||
.orElse(new HashMap<>()); | ||
} | ||
|
||
private Optional<Application> getApplication(String applicationName) { | ||
if (front50Service == null) { | ||
return Optional.empty(); | ||
} | ||
|
||
try { | ||
return Optional.of(front50Service.get(applicationName)); | ||
} catch (RetrofitError e) { | ||
if (e.getResponse().getStatus() == HttpStatus.NOT_FOUND.value()) { | ||
return Optional.empty(); | ||
} | ||
throw new SpinnakerException( | ||
format("Failed to retrieve application '%s'", applicationName), e); | ||
} catch (RuntimeException re) { | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
Oops, something went wrong.