use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.
the class TopologiesResource method getProviderConfigurations.
@GET
@Produces({ APPLICATION_JSON })
@Path(PROVIDERCONFIG_API_PATH)
public HrefListing getProviderConfigurations() {
HrefListing listing = new HrefListing();
listing.setHref(buildHref(request));
GatewayServices services = (GatewayServices) request.getServletContext().getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
List<HrefListItem> configs = new ArrayList<>();
TopologyService ts = services.getService(GatewayServices.TOPOLOGY_SERVICE);
// Get all the simple descriptor file names
for (File providerConfig : ts.getProviderConfigurations()) {
String id = FilenameUtils.getBaseName(providerConfig.getName());
configs.add(new HrefListItem(buildHref(id, request), providerConfig.getName()));
}
listing.setItems(configs);
return listing;
}
use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.
the class TopologiesResource method getTopology.
@GET
@Produces({ APPLICATION_JSON, APPLICATION_XML })
@Path(SINGLE_TOPOLOGY_API_PATH)
public Topology getTopology(@PathParam("id") String id) {
GatewayServices services = (GatewayServices) request.getServletContext().getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
GatewayConfig config = (GatewayConfig) request.getServletContext().getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
TopologyService ts = services.getService(GatewayServices.TOPOLOGY_SERVICE);
for (org.apache.knox.gateway.topology.Topology t : ts.getTopologies()) {
if (t.getName().equals(id)) {
try {
t.setUri(new URI(buildURI(t, config, request)));
} catch (URISyntaxException se) {
t.setUri(null);
}
return BeanConverter.getTopology(t);
}
}
return null;
}
use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.
the class TopologiesResource method uploadTopology.
@PUT
@Consumes({ APPLICATION_JSON, APPLICATION_XML })
@Path(SINGLE_TOPOLOGY_API_PATH)
public Topology uploadTopology(@PathParam("id") String id, Topology t) {
Topology result = null;
GatewayServices gs = (GatewayServices) request.getServletContext().getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
t.setName(id);
TopologyService ts = gs.getService(GatewayServices.TOPOLOGY_SERVICE);
// Check for existing topology with the same name, to see if it had been generated
boolean existingGenerated = false;
for (org.apache.knox.gateway.topology.Topology existingTopology : ts.getTopologies()) {
if (existingTopology.getName().equals(id)) {
existingGenerated = existingTopology.isGenerated();
break;
}
}
// out of sync with the source descriptor. Otherwise, deploy the updated version.
if (!existingGenerated) {
ts.deployTopology(BeanConverter.getTopology(t));
result = getTopology(id);
} else {
log.disallowedOverwritingGeneratedTopology(id);
}
return result;
}
use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.
the class SimpleDescriptorHandler method provisionQueryParamEncryptionCredential.
/**
* KNOX-1136
*
* Provision the query string encryption password prior to it being randomly generated during the topology
* deployment.
*
* @param topologyName The name of the topology for which the credential will be provisioned.
*
* @return true if the credential was successfully provisioned; otherwise, false.
*/
private static boolean provisionQueryParamEncryptionCredential(final String topologyName) {
boolean result = false;
try {
GatewayServices services = GatewayServer.getGatewayServices();
if (services != null) {
MasterService ms = services.getService("MasterService");
if (ms != null) {
KeystoreService ks = services.getService(GatewayServices.KEYSTORE_SERVICE);
if (ks != null) {
if (!ks.isCredentialStoreForClusterAvailable(topologyName)) {
ks.createCredentialStoreForCluster(topologyName);
}
// If the credential store existed, or it was just successfully created
if (ks.getCredentialStoreForCluster(topologyName) != null) {
AliasService aliasService = services.getService(GatewayServices.ALIAS_SERVICE);
if (aliasService != null) {
// Derive and set the query param encryption password
String queryEncryptionPass = new String(ms.getMasterSecret()) + topologyName;
aliasService.addAliasForCluster(topologyName, "encryptQueryString", queryEncryptionPass);
result = true;
}
}
}
}
}
} catch (Exception e) {
log.exceptionCreatingPasswordForEncryption(topologyName, e);
}
return result;
}
use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.
the class TokenServiceResourceTest method testDefaultTTL.
@Test
public void testDefaultTTL() throws Exception {
ServletContext context = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(context.getInitParameter("knox.token.audiences")).andReturn("recipient1,recipient2");
EasyMock.expect(context.getInitParameter("knox.token.ttl")).andReturn(null);
EasyMock.expect(context.getInitParameter("knox.token.target.url")).andReturn(null);
EasyMock.expect(context.getInitParameter("knox.token.client.data")).andReturn(null);
HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(request.getServletContext()).andReturn(context).anyTimes();
Principal principal = EasyMock.createNiceMock(Principal.class);
EasyMock.expect(principal.getName()).andReturn("alice").anyTimes();
EasyMock.expect(request.getUserPrincipal()).andReturn(principal).anyTimes();
GatewayServices services = EasyMock.createNiceMock(GatewayServices.class);
EasyMock.expect(context.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(services);
JWTokenAuthority authority = new TestJWTokenAuthority(publicKey, privateKey);
EasyMock.expect(services.getService(GatewayServices.TOKEN_SERVICE)).andReturn(authority);
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.expect(response.getWriter()).andReturn(printWriter);
EasyMock.replay(principal, services, context, request, response);
TokenResource tr = new TokenResource();
tr.request = request;
tr.response = response;
tr.context = context;
tr.init();
// Issue a token
Response retResponse = tr.doGet();
assertEquals(200, retResponse.getStatus());
// Parse the response
String retString = writer.toString();
String accessToken = getTagValue(retString, "access_token");
assertNotNull(accessToken);
String expiry = getTagValue(retString, "expires_in");
assertNotNull(expiry);
// Verify the token
JWT parsedToken = new JWTToken(accessToken);
assertEquals("alice", parsedToken.getSubject());
assertTrue(authority.verifyToken(parsedToken));
Date expiresDate = parsedToken.getExpiresDate();
Date now = new Date();
assertTrue(expiresDate.after(now));
assertTrue((expiresDate.getTime() - now.getTime()) < 30000L);
}
Aggregations