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;
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations