|
| 1 | +package org.eclipse.jifa.server.service.impl.netflix; |
| 2 | + |
| 3 | +import static org.eclipse.jifa.common.enums.CommonErrorCode.ILLEGAL_ARGUMENT; |
| 4 | +import static org.eclipse.jifa.server.enums.ServerErrorCode.ACCESS_DENIED; |
| 5 | +import static org.eclipse.jifa.server.enums.ServerErrorCode.FILE_NOT_FOUND; |
| 6 | +import static org.eclipse.jifa.server.enums.ServerErrorCode.UNAVAILABLE; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStreamReader; |
| 10 | +import java.net.URL; |
| 11 | + |
| 12 | +import javax.net.ssl.HttpsURLConnection; |
| 13 | + |
| 14 | +import org.eclipse.jifa.common.domain.exception.ErrorCode; |
| 15 | +import org.eclipse.jifa.common.util.Validate; |
| 16 | +import org.eclipse.jifa.server.domain.entity.shared.file.BaseFileEntity; |
| 17 | +import org.eclipse.jifa.server.domain.entity.shared.user.UserEntity; |
| 18 | +import org.eclipse.jifa.server.enums.ServerErrorCode; |
| 19 | +import org.eclipse.jifa.server.service.UserService; |
| 20 | +import org.springframework.beans.factory.annotation.Autowired; |
| 21 | +import org.springframework.context.annotation.Primary; |
| 22 | +import org.springframework.stereotype.Component; |
| 23 | + |
| 24 | +import com.google.gson.Gson; |
| 25 | +import com.google.gson.JsonObject; |
| 26 | +import com.google.gson.annotations.SerializedName; |
| 27 | +import com.netflix.gandalf.agent.AuthorizationClient; |
| 28 | +import com.netflix.gandalf.agent.GandalfException; |
| 29 | +import com.netflix.gandalf.agent.protogen.AuthorizationResponse; |
| 30 | +import com.netflix.metatron.ipc.security.MetatronSslContext; |
| 31 | + |
| 32 | +import lombok.extern.slf4j.Slf4j; |
| 33 | + |
| 34 | +@Primary |
| 35 | +@Component |
| 36 | +@Slf4j |
| 37 | +public class NetflixGandalfUserAccessService implements FileAccessService { |
| 38 | + |
| 39 | + private static final String flamecommanderApi = "https://flamecommander.cluster.us-east-1.test.cloud.netflix.net:7004/"; |
| 40 | + |
| 41 | + AuthorizationClient authorizationClient; |
| 42 | + |
| 43 | + @Autowired |
| 44 | + UserService userService; |
| 45 | + |
| 46 | + public NetflixGandalfUserAccessService() { |
| 47 | + this.authorizationClient = new AuthorizationClient(); |
| 48 | + this.authorizationClient.status(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void checkAuthorityForCurrentUser(BaseFileEntity file) |
| 53 | + { |
| 54 | + UserEntity user = userService.getCurrentUser(); |
| 55 | + |
| 56 | + log.info("Checking authority for file '{}' and user '{}'", |
| 57 | + file.getUniqueName(), |
| 58 | + user.getName()); |
| 59 | + |
| 60 | + InstanceCommand ic = InstanceCommand.parseParam(file.getUniqueName()); |
| 61 | + Validate.notNull(ic, ILLEGAL_ARGUMENT, "Not an s3! formed pathname: " + file.getUniqueName()); |
| 62 | + |
| 63 | + // look up historic information in flamecommander database for instance |
| 64 | + final FcEvent fcEvent; |
| 65 | + try { |
| 66 | + fcEvent = loadFlamecommanderEvent(ic); |
| 67 | + } catch (IOException e) { |
| 68 | + log.error("flamecommander api error, sending error", e); |
| 69 | + Validate.error(FILE_NOT_FOUND, "flamecommander api error: " + e.getMessage()); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + Validate.notNull(fcEvent, FILE_NOT_FOUND, |
| 74 | + String.format("No event found in flamecommander for instanceId=%s, commandId=%s", |
| 75 | + ic.instanceId, ic.commandId)); |
| 76 | + |
| 77 | + Validate.notNull(user.getName(), ACCESS_DENIED, "No user identity found"); |
| 78 | + |
| 79 | + log.debug("found identity, verifying resources against gandalf"); |
| 80 | + JsonObject subject = createGandalfSubject(user.getName()); |
| 81 | + JsonObject resource = createGandalfResource(); |
| 82 | + try |
| 83 | + { |
| 84 | + AuthorizationResponse authorizationResponse = authorizationClient.isAuthorized(fcEvent.application, |
| 85 | + fcEvent.stack, fcEvent.accountId, fcEvent.region, subject, "SSH", resource, null); |
| 86 | + log.debug("gandalf response: {}", authorizationResponse); |
| 87 | + Validate.isTrue(authorizationResponse.getAllowed(), ACCESS_DENIED, "Access denied by gandalf"); |
| 88 | + } |
| 89 | + catch (GandalfException e) |
| 90 | + { |
| 91 | + Validate.error(UNAVAILABLE, "gandalf error: " + e.getMessage()); |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + static class FcEvent { |
| 97 | + @SerializedName("account_id") |
| 98 | + String accountId; |
| 99 | + String application; |
| 100 | + String stack; |
| 101 | + String region; |
| 102 | + } |
| 103 | + |
| 104 | + String getFcProfileLookupUrl(InstanceCommand ic) { |
| 105 | + return flamecommanderApi + "api/jifa/fc-event/" + ic.instanceId + "/" + ic.commandId; |
| 106 | + } |
| 107 | + |
| 108 | + FcEvent loadFlamecommanderEvent(InstanceCommand ic) throws IOException { |
| 109 | + final String eventUrl = getFcProfileLookupUrl(ic); |
| 110 | + HttpsURLConnection connection = (HttpsURLConnection) new URL(eventUrl).openConnection(); |
| 111 | + connection.setSSLSocketFactory(MetatronSslContext.forClient("flamecommander").getSocketFactory()); |
| 112 | + connection.setHostnameVerifier((hostname, session) -> true); |
| 113 | + FcEvent[] events = new Gson().fromJson(new InputStreamReader(connection.getInputStream()), FcEvent[].class); |
| 114 | + return (events.length == 1) ? events[0] : null; |
| 115 | + } |
| 116 | + |
| 117 | + JsonObject createGandalfSubject(String email) { |
| 118 | + final JsonObject user = new JsonObject(); |
| 119 | + user.addProperty("email", email); |
| 120 | + final JsonObject subject = new JsonObject(); |
| 121 | + subject.add("user", user); |
| 122 | + return subject; |
| 123 | + } |
| 124 | + |
| 125 | + JsonObject createGandalfResource() { |
| 126 | + final JsonObject resource = new JsonObject(); |
| 127 | + resource.addProperty("username", "nfsuper"); |
| 128 | + return resource; |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments