Skip to content

Commit

Permalink
OP-20633, OP-21175: feature implementing the user logged in events AP…
Browse files Browse the repository at this point in the history
…I's and Publish user details as events to rabbitmq. (#428)
  • Loading branch information
sudhakaropsmx authored Oct 11, 2023
1 parent f8238de commit e390e6d
Show file tree
Hide file tree
Showing 14 changed files with 333 additions and 26 deletions.
8 changes: 6 additions & 2 deletions gate-web/gate-web.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ dependencies {
implementation "io.spinnaker.fiat:fiat-core:$fiatVersion"
implementation "io.spinnaker.fiat:fiat-api:$fiatVersion"

implementation "io.spinnaker.kork:kork-config"
implementation "io.spinnaker.kork:kork-plugins"
implementation "io.spinnaker.kork:kork-web"
implementation "com.netflix.frigga:frigga"
implementation "redis.clients:jedis"
implementation "com.netflix.hystrix:hystrix-core:1.5.12"

implementation "commons-io:commons-io"
implementation "org.codehaus.groovy:groovy-templates"
Expand All @@ -55,12 +57,14 @@ dependencies {


implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-openfeign', version: '2.2.4.RELEASE'
implementation 'org.apache.camel:camel-core:3.14.1'
implementation 'org.apache.camel:camel-rabbitmq:3.14.1'
implementation 'org.apache.camel:camel-jackson:3.14.1'

implementation group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'

implementation group: 'org.keycloak', name: 'keycloak-admin-client', version: '20.0.2'


runtimeOnly "io.spinnaker.kork:kork-runtime"
runtimeOnly "org.springframework.boot:spring-boot-properties-migrator"

Expand All @@ -77,6 +81,7 @@ dependencies {
testImplementation "org.springframework.security:spring-security-oauth2-jose"
testImplementation "com.unboundid:unboundid-ldapsdk"
testImplementation "io.spinnaker.kork:kork-jedis-test"
testImplementation "io.spinnaker.kork:kork-test"
testRuntimeOnly "io.spinnaker.kork:kork-retrofit"

// Add each included authz provider as a runtime dependency
Expand Down Expand Up @@ -106,5 +111,4 @@ test {
}
}
}
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ class OpsmxAuditClientServiceController {
@RequestParam(value = "sortBy", required = false) String sortBy,
@RequestParam(value = "limit", required = false) Integer limit,
@RequestParam(value = "applicationName", required = false) String applicationName,
@RequestParam(value = "agentName", required = false) String agentName) {
return opsmxAuditClientService.getDeliveryInsightCharts(version, type, source, chartId, startTime, endTime, days,
noOfDays, argoName, search, sort, page, pageLimit, sortBy,limit,applicationName,agentName)
@RequestParam(value = "agentName", required = false) String agentName,
@RequestParam(value = "sortOrder", required = false) String sortOrder,
@RequestParam(value = "filterBy", required = false) String filterBy) {
return opsmxAuditClientService.getDeliveryInsightCharts(version, type, source, chartId, startTime, endTime, days, noOfDays, argoName, search, sort, page, pageLimit, sortBy,limit,applicationName,agentName, sortOrder, filterBy)

}

@ApiOperation(value = "Endpoint for audit-client rest services")
Expand Down Expand Up @@ -190,15 +192,16 @@ class OpsmxAuditClientServiceController {
@RequestParam(value = "argoName", required = false) String argoName,
@RequestParam(value = "search", required = false) String search,
@RequestParam(value = "noOfDays", required = false) String noOfDays,
@RequestParam(value = "limit", required = false) Integer limit) {
Response response = opsmxAuditClientService.downloadDeliveryInsightsCSVFile(version, type, source, chartId, startTime, endTime, days,argoName,search,noOfDays,limit)
@RequestParam(value = "limit", required = false) Integer limit,
@RequestParam(value = "filterBy", required = false) String filterBy) {
Response response = opsmxAuditClientService.downloadDeliveryInsightsCSVFile(version, type, source, chartId, startTime, endTime, days,argoName,search,noOfDays,limit,filterBy)
log.info("response for the delivery insights endpoint:" + response.getHeaders())
if (response.getBody()!=null) {
InputStream inputStream = response.getBody().in()
try {
byte[] csvFile = IOUtils.toByteArray(inputStream)
HttpHeaders headers = new HttpHeaders()
headers.setContentType(MediaType.parseMediaType("text/csv"));
headers.setContentType(MediaType.parseMediaType("text/csv"))
headers.add("Content-Disposition", response.getHeaders().stream().filter({ header -> header.getName().trim().equalsIgnoreCase("Content-Disposition") }).collect(Collectors.toList()).get(0).value)
return ResponseEntity.ok().headers(headers).body(csvFile)
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ interface OpsmxAuditClientService {
@Query("sortBy") String sortBy,
@Query("limit") Integer limit,
@Query("applicationName") String applicationName,
@Query("agentName") String agentName)
@Query("agentName") String agentName,
@Query("sortOrder") String sortOrder,
@Query("filterBy") String filterBy)

@GET("/auditclientservice/{version}/{type}/{source}/{source1}")
Object getAuditClientResponse3(@Path('version') String version,
Expand Down Expand Up @@ -127,5 +129,6 @@ interface OpsmxAuditClientService {
@Query('argoName') String argoName,
@Query('search') String search,
@Query("noOfDays") String noOfDays,
@Query('limit') Integer limit)
@Query('limit') Integer limit,
@Query('filterBy') String filterBy)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2021 OpsMx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.gate.config;

import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.impl.DefaultCamelContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@ConditionalOnExpression("${message-broker.enabled:true}")
public class CamelConfig {

@Autowired private UserActivityRouteBuilder userActivityRouteBuilder;

@Bean
public CamelContext camelContext() throws Exception {
CamelContext camelContext = new DefaultCamelContext();
camelContext.addRoutes(userActivityRouteBuilder);
camelContext.getShutdownStrategy().setShutdownNowOnTimeout(true);
camelContext.getShutdownStrategy().setTimeout(5);
camelContext.getShutdownStrategy().setTimeUnit(TimeUnit.SECONDS);
camelContext.start();
return camelContext;
}

@Bean
public ProducerTemplate producerTemplate(CamelContext camelContext) {
return camelContext.createProducerTemplate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2021 OpsMx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.gate.config;

public interface CamelRouteConfig {
String getUserActivityQueueEndPoint();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2021 OpsMx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.gate.config;

import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix = "message-broker")
@ConditionalOnExpression("${message-broker.enabled:true}")
@EnableConfigurationProperties({
MessageBrokerProperties.class,
MessageBrokerProperties.Endpoint.class
})
public class MessageBrokerProperties {

private boolean enabled;
private String username;
private String password;
private String host;
private String port;
private Endpoint endpoint;

@Data
@Configuration
@ConditionalOnExpression("${message-broker.enabled:true}")
@ConfigurationProperties(prefix = "message-broker.endpoint")
public static class Endpoint {
private String name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2021 OpsMx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.gate.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnExpression("${message-broker.enabled:true}")
@ConditionalOnProperty(value = "message-broker.endpoint.name", havingValue = "rabbitmq")
public class RabbitMQConfig implements CamelRouteConfig {

@Autowired private MessageBrokerProperties messageBrokerProperties;
private static final String isdExchange = "isd.userLoginDetails";
private String directUserActivity = "userLoginDetails";

@Override
public String getUserActivityQueueEndPoint() {
return messageBrokerProperties.getEndpoint().getName()
+ ":"
+ isdExchange
+ "?queue="
+ directUserActivity
+ "&autoDelete=false&routingKey="
+ directUserActivity
+ "&declare=true&durable=true&exchangeType=direct&hostname="
+ messageBrokerProperties.getHost()
+ "&portNumber="
+ messageBrokerProperties.getPort()
+ "&username="
+ messageBrokerProperties.getUsername()
+ "&password="
+ messageBrokerProperties.getPassword();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2021 OpsMx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.gate.config;

import static com.opsmx.spinnaker.gate.constant.CamelEndpointConstant.directUserActivity;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnExpression("${message-broker.enabled:true}")
public class UserActivityRouteBuilder extends RouteBuilder {

private final String userActivity = "userActivity";
@Autowired private CamelRouteConfig camelRouteConfig;

@Override
public void configure() throws Exception {

from(directUserActivity)
.id(userActivity)
.to(camelRouteConfig.getUserActivityQueueEndPoint())
.end();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@

public interface AuditHandler {

void publishEvent(AuditEventType auditEventType, Object auditData);
String publishEvent(AuditEventType auditEventType, Object auditData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package com.opsmx.spinnaker.gate.audit;

import com.google.gson.Gson;
import com.opsmx.spinnaker.gate.enums.AuditEventType;
import com.opsmx.spinnaker.gate.feignclient.AuditService;
import com.opsmx.spinnaker.gate.model.OesAuditModel;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.cloud.openfeign.EnableFeignClients;
Expand All @@ -28,16 +30,21 @@
@Component
@EnableFeignClients(basePackageClasses = AuditService.class)
@ConditionalOnExpression("${services.auditservice.enabled:true}")
@Slf4j
public class AuditRestApiHandler implements AuditHandler {

@Autowired private AuditService auditService;
Gson gson = new Gson();

@Override
public void publishEvent(AuditEventType auditEventType, Object auditData) {
public String publishEvent(AuditEventType auditEventType, Object auditData) {
OesAuditModel oesAuditModel = new OesAuditModel();
oesAuditModel.setEventId(UUID.randomUUID().toString());
oesAuditModel.setAuditData(auditData);
oesAuditModel.setEventType(auditEventType);
auditService.publishAuditData(oesAuditModel, "OES");
String model = gson.toJson(oesAuditModel, OesAuditModel.class);
log.debug("model: {}", model);
return model;
}
}
Loading

0 comments on commit e390e6d

Please sign in to comment.