Skip to content

Commit

Permalink
WIP | Fixing operator issues (#11)
Browse files Browse the repository at this point in the history
Fixed operator issues
  • Loading branch information
sondre81 authored Oct 20, 2023
1 parent 1af67ff commit e37c876
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
22 changes: 21 additions & 1 deletion src/main/java/no/fintlabs/portal/model/client/ClientService.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public ClientService(ClientFactory clientFactory, LdapService ldapService, Asset
public Optional<Client> addClient(Client client, Organisation organisation) {
clientFactory.setupClient(client, organisation);

log.debug("Add client: ClientId: {}, ClientName: {}, Dn: {}", client.getClientId(), client.getName(), client.getDn());

OAuthClient oAuthClient = namOAuthClientService.addOAuthClient(
String.format("c_%s", client.getName()
.replace("@", "_")
Expand All @@ -69,7 +71,14 @@ public Optional<Client> addClient(Client client, Organisation organisation) {
.map(createdClient -> {
createdClient.setPublicKey(client.getPublicKey());
resetAndEncryptPassword(createdClient, createdClient.getPublicKey());
encryptClientSecret(createdClient, createdClient.getPublicKey());

try {
encryptClientSecret(createdClient, createdClient.getPublicKey());
} catch (Exception e){
log.error("Error in encrypt client secret ", e);
createdClient.setClientSecret(null);
}

db.save(createdClient);

return createdClient;
Expand Down Expand Up @@ -127,6 +136,17 @@ public Optional<Client> getClientByDnFromLdap(String dn) {
}

public Optional<Client> updateClient(Client client) {

if (!StringUtils.hasText(client.getPassword()) && StringUtils.hasText(client.getPublicKey())) {
resetAndEncryptPassword(client, client.getPublicKey());
log.warn("Get password because it's empty");
}

if (!StringUtils.hasText(client.getClientSecret()) && StringUtils.hasText(client.getPublicKey())) {
encryptClientSecret(client, client.getPublicKey());
log.warn("Get clientSecret from nam because it's empty");
}

if (ldapService.updateEntry(client)) {
return getClientByDnFromLdap(client.getDn())
.map(updatedClient -> db.findById(LdapNameBuilder.newInstance(Objects.requireNonNull(updatedClient.getDn())).build())
Expand Down
27 changes: 23 additions & 4 deletions src/main/java/no/fintlabs/portal/oauth/NamOAuthClientService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
@Slf4j
public class NamOAuthClientService {

public static final int RETRY_ATTEMPTS = 10;
public static final int RETRY_SLEEP_MS = 350;
@Autowired
private ObjectMapper mapper;
@Value("${fint.nam.oauth.username}")
Expand Down Expand Up @@ -89,11 +91,28 @@ public void removeOAuthClient(String clientId) {

public OAuthClient getOAuthClient(String clientId) {
log.info("Fetching client {}...", clientId);
for (int i = 1; true; i++) {
try {
return restTemplate.getForObject(NamOAuthConstants.CLIENT_URL_TEMPLATE, OAuthClient.class, idpHostname, clientId);
} catch (Exception e) {
log.error("Unable to get client {}, this was iteration number {}", clientId, i);
log.error("Error: ", e);

if (i == RETRY_ATTEMPTS) {
log.info("Failed to getOauthClient after max retry attempts. Giving up");
throw e;
}

sleep(i);
}
}
}

private void sleep(int i) {
try {
return restTemplate.getForObject(NamOAuthConstants.CLIENT_URL_TEMPLATE, OAuthClient.class, idpHostname, clientId);
} catch (Exception e) {
log.error("Unable to get client {}", clientId, e);
throw e;
Thread.sleep(i * RETRY_SLEEP_MS);
} catch (InterruptedException ex) {
log.debug("Usually doesn't happen", ex);
}
}
}
6 changes: 3 additions & 3 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ spring:

logging:
level:
org.apache.kafka: OFF
no.fintlabs.kafka: OFF
org.springframework.kafka: OFF
org.apache.kafka: WARN
no.fintlabs.kafka: WARN
org.springframework.kafka: WARN
no.fintlabs: DEBUG

0 comments on commit e37c876

Please sign in to comment.