Skip to content

Commit

Permalink
Add support for local OpenSearch in development environment
Browse files Browse the repository at this point in the history
Added a new development configuration for OpenSearch and adjusted production configuration accordingly. This change allows running the application locally with an instance of OpenSearch, which can be useful for local testing and development. Modifies application, development and production YAMLs.
  • Loading branch information
krharum committed Feb 15, 2024
1 parent f6fbd8e commit c7cd4c6
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.nav.dolly.bestilling.aareg.amelding;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.core.util.Json;
import lombok.extern.slf4j.Slf4j;
import no.nav.dolly.bestilling.aareg.command.AmeldingPutCommand;
import no.nav.dolly.config.Consumers;
Expand Down Expand Up @@ -64,8 +65,9 @@ public Flux<String> sendAmeldinger(List<AMeldingDTO> ameldinger, String miljoe)
.collect(Collectors.joining(",")))));
} else {
log.info("Sender Amelding {} til miljø {}: {}",
amelding.getKalendermaaned().format(YEAR_MONTH), miljoe, amelding);
amelding.getKalendermaaned().format(YEAR_MONTH), miljoe, Json.pretty(amelding));
return new AmeldingPutCommand(webClient, amelding, miljoe, token.getTokenValue()).call()
.doOnNext(status -> log.info("Ameldingstatus: {}", status.getStatusCode()))
.map(status -> status.getStatusCode().is2xxSuccessful() ? "OK" :
errorStatusDecoder.getErrorText(HttpStatus.valueOf(status.getStatusCode().value()), status.getBody()));
}
Expand Down
4 changes: 4 additions & 0 deletions apps/oppsummeringsdokument-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ Applikasjonen kan nås fra [/swagger](https://oppsummeringsdokument-service.inte
```
-Dspring.cloud.vault.token={VAULT_TOKEN} -Dspring.profiles.active=dev
```

For å kjøre lokalt med opensearch:

docker run -p 9200:9200 -p 9600:9600 -e "discovery.type=single-node" -e "plugins.security.disabled=true" --name opensearch-node -d opensearchproject/opensearch:latest
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package no.nav.testnav.apps.oppsummeringsdokumentservice.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.opensearch.client.RestHighLevelClient;
import org.opensearch.data.client.orhlc.AbstractOpenSearchConfiguration;
import org.opensearch.data.client.orhlc.ClientConfiguration;
import org.opensearch.data.client.orhlc.RestClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

import java.time.Duration;

@Slf4j
@Configuration
@Profile("dev")
public class OpenSearchLocalConfig extends AbstractOpenSearchConfiguration {

@Value("${open.search.uri}")
private String uri;

@Override
@SuppressWarnings("java:S2095")
public RestHighLevelClient opensearchClient() {

return RestClients.create(ClientConfiguration.builder()
.connectedTo(uri.replace("http://", ""))
.withConnectTimeout(Duration.ofSeconds(10))
.withSocketTimeout(Duration.ofSeconds(5))
.build())
.rest();
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
package no.nav.testnav.apps.oppsummeringsdokumentservice.config;

import lombok.RequiredArgsConstructor;
import no.nav.testnav.apps.oppsummeringsdokumentservice.config.credentials.ElasticSearchCredentials;
import org.opensearch.client.RestHighLevelClient;
import org.opensearch.data.client.orhlc.AbstractOpenSearchConfiguration;
import org.opensearch.data.client.orhlc.ClientConfiguration;
import org.opensearch.data.client.orhlc.RestClients;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import java.time.Duration;

@Profile("prod")
@Configuration
@RequiredArgsConstructor
public class OpensearchConfig extends AbstractOpenSearchConfiguration {
private final ElasticSearchCredentials elasticSearchCredentials;

@Value("${open.search.username}")
private String username;

@Value("${open.search.password}")
private String password;

@Value("${open.search.uri}")
private String uri;

@Override
@Bean
@SuppressWarnings("java:S2095")
public RestHighLevelClient opensearchClient() {
ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo(elasticSearchCredentials.getEndpoints().replace("https://", ""))
.usingSsl()
.withBasicAuth(elasticSearchCredentials.getUsername(), elasticSearchCredentials.getPassword())
.withConnectTimeout(Duration.ofSeconds(10))
.withSocketTimeout(Duration.ofSeconds(5))
.build();

return RestClients.create(clientConfiguration).rest();

return RestClients.create(ClientConfiguration.builder()
.connectedTo(uri.replace("https://", ""))
.usingSsl()
.withBasicAuth(username, password)
.withConnectTimeout(Duration.ofSeconds(10))
.withSocketTimeout(Duration.ofSeconds(5))
.build())
.rest();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

open:
search:
uri: http://localhost:9200
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

spring:
security:
oauth2:
resourceserver:
tokenx:
issuer-uri: ${TOKEN_X_ISSUER}
jwk-set-uri: ${TOKEN_X_JWKS_URI}
accepted-audience: ${TOKEN_X_CLIENT_ID}
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
AAD_ISSUER_URI: https://login.microsoftonline.com/62366534-1ec3-4962-8869-9b5535279d0b

spring:
data:
elasticsearch:
client:
reactive:
username: ${OPEN_SEARCH_USERNAME}
password: ${OPEN_SEARCH_PASSWORD}
endpoints: ${OPEN_SEARCH_URI}
use-ssl: true
security:
oauth2:
resourceserver:
aad:
issuer-uri: ${AAD_ISSUER_URI}/v2.0
jwk-set-uri: ${AAD_ISSUER_URI}/discovery/v2.0/keys
accepted-audience: ${azure.app.client.id}, api://${azure.app.client.id}
tokenx:
issuer-uri: ${TOKEN_X_ISSUER}
jwk-set-uri: ${TOKEN_X_JWKS_URI}
accepted-audience: ${TOKEN_X_CLIENT_ID}
application:
name: oppsummeringsdokument-service
version: application.version.todo #TODO Finn ut hvordan denne kan settes fra gradle
description: Tjeneste for å sende inn oppsummeringsdokument til aareg og søke i innsendte oppsummeringsdokumenter.
elasticsearch:
rest:
username: ${OPEN_SEARCH_USERNAME}
password: ${OPEN_SEARCH_PASSWORD}
uris: ${OPEN_SEARCH_URI}
cloud:
vault:
enabled: false
Expand Down

0 comments on commit c7cd4c6

Please sign in to comment.