use of com.redhat.cloud.notifications.models.BasicAuthentication in project notifications-backend by RedHatInsights.
the class CamelTypeProcessorTest method buildCamelEndpoint.
private static Endpoint buildCamelEndpoint(String accountId) {
BasicAuthentication basicAuth = new BasicAuthentication("john", "doe");
CamelProperties properties = new CamelProperties();
properties.setUrl("https://redhat.com");
properties.setDisableSslVerification(TRUE);
properties.setSecretToken("top-secret");
properties.setBasicAuthentication(basicAuth);
properties.setExtras(Map.of("foo", "bar"));
// Todo: NOTIF-429 backward compatibility change - Remove soon.
properties.setSubType(SUB_TYPE);
Endpoint endpoint = new Endpoint();
endpoint.setAccountId(accountId);
endpoint.setType(CAMEL);
endpoint.setSubType(SUB_TYPE);
endpoint.setProperties(properties);
return endpoint;
}
use of com.redhat.cloud.notifications.models.BasicAuthentication in project notifications-backend by RedHatInsights.
the class EndpointResourceTest method testWebhookAttributes.
@Test
void testWebhookAttributes() {
String tenant = "testWebhookAttributes";
String orgId = "testWebhookAttributes2";
String userName = "user";
String identityHeaderValue = TestHelpers.encodeRHIdentityInfo(tenant, orgId, userName);
Header identityHeader = TestHelpers.createRHIdentityHeader(identityHeaderValue);
MockServerConfig.addMockRbacAccess(identityHeaderValue, MockServerConfig.RbacAccess.FULL_ACCESS);
// Add new endpoints
WebhookProperties properties = new WebhookProperties();
properties.setMethod(HttpType.POST);
properties.setDisableSslVerification(false);
properties.setSecretToken("my-super-secret-token");
properties.setBasicAuthentication(new BasicAuthentication("myuser", "mypassword"));
properties.setUrl(getMockServerUrl());
Endpoint ep = new Endpoint();
ep.setType(EndpointType.WEBHOOK);
ep.setName("endpoint to find");
ep.setDescription("needle in the haystack");
ep.setEnabled(true);
ep.setProperties(properties);
Response response = given().header(identityHeader).when().contentType(JSON).body(Json.encode(ep)).post("/endpoints").then().statusCode(200).contentType(JSON).extract().response();
JsonObject responsePoint = new JsonObject(response.getBody().asString());
responsePoint.mapTo(Endpoint.class);
assertNotNull(responsePoint.getString("id"));
// Fetch single endpoint also and verify
JsonObject responsePointSingle = fetchSingle(responsePoint.getString("id"), identityHeader);
assertNotNull(responsePoint.getJsonObject("properties"));
assertTrue(responsePointSingle.getBoolean("enabled"));
assertNotNull(responsePointSingle.getJsonObject("properties"));
assertNotNull(responsePointSingle.getJsonObject("properties").getString("secret_token"));
JsonObject attr = responsePointSingle.getJsonObject("properties");
attr.mapTo(WebhookProperties.class);
assertNotNull(attr.getJsonObject("basic_authentication"));
assertEquals("mypassword", attr.getJsonObject("basic_authentication").getString("password"));
}
use of com.redhat.cloud.notifications.models.BasicAuthentication in project notifications-backend by RedHatInsights.
the class EndpointResourceTest method addCamelEndpoint.
@Test
void addCamelEndpoint() {
String tenant = "empty";
String orgId = "empty";
String userName = "user";
String identityHeaderValue = TestHelpers.encodeRHIdentityInfo(tenant, orgId, userName);
Header identityHeader = TestHelpers.createRHIdentityHeader(identityHeaderValue);
MockServerConfig.addMockRbacAccess(identityHeaderValue, MockServerConfig.RbacAccess.FULL_ACCESS);
CamelProperties cAttr = new CamelProperties();
cAttr.setDisableSslVerification(false);
cAttr.setUrl(getMockServerUrl());
cAttr.setBasicAuthentication(new BasicAuthentication("testuser", "secret"));
Map<String, String> extras = new HashMap<>();
extras.put("template", "11");
cAttr.setExtras(extras);
Endpoint ep = new Endpoint();
ep.setType(EndpointType.CAMEL);
ep.setSubType("ansible");
ep.setName("Push the camel through the needle's ear");
ep.setDescription("How many humps has a camel?");
ep.setEnabled(true);
ep.setProperties(cAttr);
String responseBody = given().header(identityHeader).when().contentType(JSON).body(Json.encode(ep)).post("/endpoints").then().statusCode(200).contentType(JSON).extract().asString();
JsonObject responsePoint = new JsonObject(responseBody);
responsePoint.mapTo(Endpoint.class);
String id = responsePoint.getString("id");
assertNotNull(id);
try {
JsonObject endpoint = fetchSingle(id, identityHeader);
JsonObject properties = responsePoint.getJsonObject("properties");
assertNotNull(properties);
assertTrue(endpoint.getBoolean("enabled"));
assertEquals("ansible", endpoint.getString("sub_type"));
// Todo: NOTIF-429 backward compatibility change - Remove soon.
assertEquals("ansible", properties.getString("sub_type"));
JsonObject extrasObject = properties.getJsonObject("extras");
assertNotNull(extrasObject);
String template = extrasObject.getString("template");
assertEquals("11", template);
JsonObject basicAuth = properties.getJsonObject("basic_authentication");
assertNotNull(basicAuth);
String user = basicAuth.getString("username");
String pass = basicAuth.getString("password");
assertEquals("testuser", user);
assertEquals("secret", pass);
} finally {
given().header(identityHeader).when().delete("/endpoints/" + id).then().statusCode(204).extract().body().asString();
}
}
use of com.redhat.cloud.notifications.models.BasicAuthentication in project notifications-backend by RedHatInsights.
the class EndpointResourceTest method addBogusCamelEndpoint.
@Test
void addBogusCamelEndpoint() {
String tenant = "empty";
String orgId = "empty";
String userName = "user";
String identityHeaderValue = TestHelpers.encodeRHIdentityInfo(tenant, orgId, userName);
Header identityHeader = TestHelpers.createRHIdentityHeader(identityHeaderValue);
MockServerConfig.addMockRbacAccess(identityHeaderValue, MockServerConfig.RbacAccess.FULL_ACCESS);
CamelProperties cAttr = new CamelProperties();
cAttr.setDisableSslVerification(false);
cAttr.setUrl(getMockServerUrl());
cAttr.setBasicAuthentication(new BasicAuthentication("testuser", "secret"));
Map<String, String> extras = new HashMap<>();
extras.put("template", "11");
cAttr.setExtras(extras);
// This is bogus because it has no sub_type
Endpoint ep = new Endpoint();
ep.setType(EndpointType.CAMEL);
ep.setName("Push the camel through the needle's ear");
ep.setDescription("How many humps has a camel?");
ep.setEnabled(true);
ep.setProperties(cAttr);
given().header(identityHeader).when().contentType(JSON).body(Json.encode(ep)).post("/endpoints").then().statusCode(400);
endpointResource.obEnabled = true;
given().header(identityHeader).when().contentType(JSON).body(Json.encode(ep)).post("/endpoints").then().statusCode(400);
endpointResource.obEnabled = false;
}
use of com.redhat.cloud.notifications.models.BasicAuthentication in project notifications-backend by RedHatInsights.
the class CamelTypeProcessor method process.
private NotificationHistory process(Notification item) {
Endpoint endpoint = item.getEndpoint();
String subType = endpoint.getSubType();
Counter processedCount = registry.counter(PROCESSED_COUNTER_NAME, "subType", subType);
processedCount.increment();
CamelProperties properties = (CamelProperties) endpoint.getProperties();
Map<String, String> metaData = new HashMap<>();
metaData.put("trustAll", String.valueOf(properties.getDisableSslVerification()));
metaData.put("url", properties.getUrl());
metaData.put("type", subType);
if (properties.getSecretToken() != null && !properties.getSecretToken().isBlank()) {
metaData.put(TOKEN_HEADER, properties.getSecretToken());
}
BasicAuthentication basicAuthentication = properties.getBasicAuthentication();
if (basicAuthentication != null && basicAuthentication.getUsername() != null && basicAuthentication.getPassword() != null) {
StringBuilder sb = new StringBuilder(basicAuthentication.getUsername());
sb.append(":");
sb.append(basicAuthentication.getPassword());
String b64 = Base64Utils.encode(sb.toString());
metaData.put("basicAuth", b64);
}
metaData.put("extras", new MapConverter().convertToDatabaseColumn(properties.getExtras()));
String originalEventId = "-not provided-";
if (item.getEvent().getId() != null) {
originalEventId = item.getEvent().getId().toString();
}
metaData.put("_originalId", originalEventId);
JsonObject payload = transformer.transform(item.getEvent().getAction());
UUID historyId = UUID.randomUUID();
JsonObject metadataAsJson = new JsonObject();
payload.put(NOTIF_METADATA_KEY, metadataAsJson);
metaData.forEach(metadataAsJson::put);
return callCamel(item, historyId, payload, originalEventId);
}
Aggregations