Search in sources :

Example 1 with ServiceAuthorization

use of org.platformlayer.xaas.model.ServiceAuthorization in project platformlayer by platformlayer.

the class OpsContextBuilder method buildOpsContext.

public OpsContext buildOpsContext(ActiveJobExecution activeJob) throws OpsException {
    ServiceType serviceType = activeJob.getServiceType();
    ProjectAuthorization projectAuthz = activeJob.getProjectAuthorization();
    List<ProjectAuthorization> projects = Lists.newArrayList();
    // .getProject();
    ProjectAuthorization runAsProject = projectAuthz;
    projects.add(runAsProject);
    MultitenantConfiguration multitenant = opsSystem.getMultitenantConfiguration();
    if (multitenant != null) {
        ProjectAuthorization masterProject = multitenant.getMasterProject();
        if (runAsProject.getName().equals(masterProject.getName())) {
            // We're in the master project
            multitenant = null;
        } else {
            runAsProject = masterProject;
            projects.add(runAsProject);
        }
    }
    TypedPlatformLayerClient defaultClient = buildClient(runAsProject);
    FederationConfiguration federationMapConfig = FederatedPlatformLayerClient.buildFederationConfiguration(defaultClient);
    FederationMap federationMap = new FederationMap(httpStrategy, mapper, federationMapConfig);
    if (multitenant != null) {
        // .getProject();
        ProjectAuthorization localProject = projectAuthz;
        TypedPlatformLayerClient localClient = buildClient(localProject);
        FederationKey host = FederationKey.LOCAL;
        ProjectId project = localClient.getProject();
        FederationMapping mapKey = new FederationMapping(host, project);
        federationMap.addMapping(mapKey, localClient);
        for (PlatformLayerKey mappedService : multitenant.getMappedItems()) {
            FederationMap.Rule rule = new FederationMap.Rule();
            rule.mappedItems = mappedService;
            rule.targetKey = mapKey;
            federationMap.addRule(rule);
        }
    }
    ProjectId runAsProjectId = new ProjectId(runAsProject.getName());
    PlatformLayerClient platformLayerClient;
    if (federationMap.isEmpty()) {
        platformLayerClient = defaultClient;
    } else {
        federationMap.addDefault(defaultClient);
        platformLayerClient = FederatedPlatformLayerClient.build(runAsProjectId, federationMap);
    }
    ServiceConfiguration serviceConfiguration = new ServiceConfiguration(runAsProjectId, serviceType);
    ServiceAuthorization serviceAuthorization;
    try {
        serviceAuthorization = serviceAuthorizationService.findServiceAuthorization(serviceType, runAsProjectId);
        // }
        if (serviceAuthorization == null) {
            serviceAuthorization = new ServiceAuthorization();
            serviceAuthorization.serviceType = serviceConfiguration.getServiceType().getKey();
        }
    } catch (RepositoryException e) {
        throw new OpsException("Error reading from repository", e);
    }
    // OpsConfig opsConfig = OpsConfig.build(serviceAuthorization);
    // UserInfo userInfo = new SimpleUserInfo(auth, opsConfig);
    OpsContext opsContext = new OpsContext(opsSystem, activeJob, serviceConfiguration, platformLayerClient, projects);
    return opsContext;
}
Also used : TypedPlatformLayerClient(org.platformlayer.TypedPlatformLayerClient) OpsException(org.platformlayer.ops.OpsException) ProjectId(org.platformlayer.ids.ProjectId) FederationMap(org.platformlayer.federation.FederationMap) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) RepositoryException(org.platformlayer.RepositoryException) OpsContext(org.platformlayer.ops.OpsContext) MultitenantConfiguration(org.platformlayer.ops.MultitenantConfiguration) FederationKey(org.platformlayer.ids.FederationKey) ServiceAuthorization(org.platformlayer.xaas.model.ServiceAuthorization) DirectPlatformLayerClient(org.platformlayer.ops.DirectPlatformLayerClient) FederatedPlatformLayerClient(org.platformlayer.federation.FederatedPlatformLayerClient) PlatformLayerClient(org.platformlayer.PlatformLayerClient) TypedPlatformLayerClient(org.platformlayer.TypedPlatformLayerClient) FederationConfiguration(org.platformlayer.federation.model.FederationConfiguration) ServiceConfiguration(org.platformlayer.ops.ServiceConfiguration) ServiceType(org.platformlayer.ids.ServiceType) ProjectAuthorization(org.platformlayer.model.ProjectAuthorization) FederationMapping(org.platformlayer.federation.FederationMapping)

Example 2 with ServiceAuthorization

use of org.platformlayer.xaas.model.ServiceAuthorization in project platformlayer by platformlayer.

the class JdbcServiceAuthorizationRepository method findServiceAuthorization.

@Override
@JdbcTransaction
public ServiceAuthorization findServiceAuthorization(ServiceType serviceType, ProjectId project) throws RepositoryException {
    try {
        JdbcConnection connection = connectionProvider.get();
        int serviceId = JdbcRepositoryHelpers.getServiceKey(connection, serviceType);
        int projectId = JdbcRepositoryHelpers.getProjectKey(connection, project);
        String sql = "SELECT data FROM service_authorizations WHERE service=? and project=?";
        List<ServiceAuthorization> items = Lists.newArrayList();
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet rs = null;
        try {
            ps.setInt(1, serviceId);
            ps.setInt(2, projectId);
            rs = ps.executeQuery();
            while (rs.next()) {
                items.add(mapRow(serviceType, rs));
            }
        } finally {
            JdbcUtils.safeClose(rs);
            JdbcUtils.safeClose(ps);
        }
        if (items.size() == 0) {
            return null;
        }
        if (items.size() != 1) {
            throw new IllegalStateException("Found duplicate results for primary key: " + serviceType + ":" + project);
        }
        return items.get(0);
    } catch (SQLException e) {
        throw new RepositoryException("Error running query", e);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) JdbcConnection(com.fathomdb.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) RepositoryException(org.platformlayer.RepositoryException) ServiceAuthorization(org.platformlayer.xaas.model.ServiceAuthorization) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 3 with ServiceAuthorization

use of org.platformlayer.xaas.model.ServiceAuthorization in project platformlayer by platformlayer.

the class ServiceAuthorizationResource method createService.

// We deliberately don't support this at the moment... it's quite restrictive on our data store
// @GET
// @Produces({ APPLICATION_XML, APPLICATION_JSON })
// public <T> ServiceAuthorizationCollection getAll() {
// List<ServiceAuthorization> items = authorizationRepository.getByAccountId(getAccountId());
// ServiceAuthorizationCollection collection = new ServiceAuthorizationCollection();
// collection.items = items;
// return collection;
// }
@POST
@Consumes({ XML, JSON })
@Produces({ XML, JSON })
public <T> ServiceAuthorization createService(final ServiceAuthorization authorization) throws OpsException, RepositoryException {
    ServiceType serviceType = getServiceType();
    authorization.serviceType = serviceType.getKey();
    final ServiceProvider serviceProvider = opsSystem.getServiceProvider(serviceType);
    if (serviceProvider == null) {
        log.warn("Unknown serviceProvider: " + serviceType);
        throw new WebApplicationException(404);
    }
    String data = authorization.data;
    if (Strings.isNullOrEmpty(data)) {
        throw new IllegalArgumentException("Data is required");
    }
    data = data.trim();
    if (data.startsWith("{")) {
        // Convert to XML
        SettingCollection settings = new SettingCollection();
        settings.items = Lists.newArrayList();
        // We presume it's a simple map of keys and values
        try {
            JSONObject json = new JSONObject(data);
            @SuppressWarnings("unchecked") Iterator<String> keys = json.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                String value = json.getString(key);
                Setting setting = new Setting();
                setting.key = key;
                setting.value = value;
                settings.items.add(setting);
            }
        } catch (JSONException e) {
            throw new IllegalArgumentException("Error parsing data", e);
        }
        JaxbHelper jaxbHelper = JaxbHelper.get(SettingCollection.class);
        String xml;
        try {
            xml = jaxbHelper.marshal(settings, false);
        } catch (JAXBException e) {
            throw new IllegalArgumentException("Error converting JSON to XML", e);
        }
        authorization.data = xml;
    }
    // Authentication authentication = getAuthentication();
    //
    // OpsContextBuilder opsContextBuilder = opsSystem.getInjector().getInstance(OpsContextBuilder.class);
    // final OpsContext opsContext = opsContextBuilder.buildOpsContext(serviceType, authentication, false);
    //
    // OpsContext.runInContext(opsContext, new CheckedCallable<Object, Exception>() {
    // @Override
    // public Object call() throws Exception {
    // serviceProvider.validateAuthorization(authorization);
    // return null;
    // }
    // });
    // serviceProvider.validateAuthorization(authorization);
    ServiceAuthorization created = authorizationRepository.createAuthorization(getProject(), authorization);
    // For security, never return the data
    created.data = null;
    return created;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) SettingCollection(org.platformlayer.xaas.model.SettingCollection) Setting(org.platformlayer.xaas.model.Setting) JAXBException(javax.xml.bind.JAXBException) JSONException(org.json.JSONException) ServiceAuthorization(org.platformlayer.xaas.model.ServiceAuthorization) JSONObject(org.json.JSONObject) ServiceType(org.platformlayer.ids.ServiceType) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) JaxbHelper(org.platformlayer.xml.JaxbHelper) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 4 with ServiceAuthorization

use of org.platformlayer.xaas.model.ServiceAuthorization in project platformlayer by platformlayer.

the class JdbcServiceAuthorizationRepository method mapRow.

static ServiceAuthorization mapRow(ServiceType serviceType, ResultSet rs) throws SQLException {
    String data = rs.getString("data");
    ServiceAuthorization authorization = new ServiceAuthorization();
    authorization.data = data;
    authorization.serviceType = serviceType.getKey();
    return authorization;
}
Also used : ServiceAuthorization(org.platformlayer.xaas.model.ServiceAuthorization)

Example 5 with ServiceAuthorization

use of org.platformlayer.xaas.model.ServiceAuthorization in project platformlayer by platformlayer.

the class ServiceAuthorizationResource method retrieveItem.

@GET
@Produces({ XML, JSON })
public ServiceAuthorization retrieveItem() throws RepositoryException {
    ServiceType serviceType = getServiceType();
    ServiceAuthorization auth = authorizationRepository.findServiceAuthorization(serviceType, getProject());
    if (auth == null) {
        throw new WebApplicationException(404);
    }
    // For security, never return the data
    auth.data = null;
    return auth;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ServiceType(org.platformlayer.ids.ServiceType) ServiceAuthorization(org.platformlayer.xaas.model.ServiceAuthorization) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

ServiceAuthorization (org.platformlayer.xaas.model.ServiceAuthorization)5 ServiceType (org.platformlayer.ids.ServiceType)3 Produces (javax.ws.rs.Produces)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 RepositoryException (org.platformlayer.RepositoryException)2 JdbcConnection (com.fathomdb.jdbc.JdbcConnection)1 JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 JAXBException (javax.xml.bind.JAXBException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 PlatformLayerClient (org.platformlayer.PlatformLayerClient)1 TypedPlatformLayerClient (org.platformlayer.TypedPlatformLayerClient)1 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)1 FederatedPlatformLayerClient (org.platformlayer.federation.FederatedPlatformLayerClient)1