Skip to content

Commit

Permalink
Diverse rydding i headere+mdc og retry tokenhenting (#1395)
Browse files Browse the repository at this point in the history
  • Loading branch information
jolarsen authored Dec 3, 2024
1 parent 6120270 commit 22e6331
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 184 deletions.
4 changes: 4 additions & 0 deletions felles/auth-filter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<groupId>no.nav.foreldrepenger.felles</groupId>
<artifactId>felles-log</artifactId>
</dependency>
<dependency>
<groupId>no.nav.foreldrepenger.felles</groupId>
<artifactId>felles-klient</artifactId>
</dependency>
<dependency>
<groupId>no.nav.foreldrepenger.felles</groupId>
<artifactId>felles-oidc</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.slf4j.MDC;

import no.nav.vedtak.exception.TekniskException;
import no.nav.vedtak.klient.http.CommonHttpHeaders;
import no.nav.vedtak.log.mdc.FnrUtils;
import no.nav.vedtak.log.mdc.MDCOperations;
import no.nav.vedtak.sikkerhet.kontekst.BasisKontekst;
Expand Down Expand Up @@ -90,13 +91,17 @@ public static void fjernKontekst() {
}

private static void setCallAndConsumerId(ContainerRequestContext request) {
String callId = Optional.ofNullable(request.getHeaderString(MDCOperations.HTTP_HEADER_CALL_ID))
.or(() -> Optional.ofNullable(request.getHeaderString(MDCOperations.HTTP_HEADER_ALT_CALL_ID)))
String callId = getHeader(request, CommonHttpHeaders.HEADER_NAV_CALLID)
.or(() -> getHeader(request, CommonHttpHeaders.HEADER_NAV_ALT_CALLID))
.or(() -> getHeader(request, CommonHttpHeaders.HEADER_NAV_LOWER_CALL_ID))
.orElseGet(MDCOperations::generateCallId);
MDCOperations.putCallId(callId);

Optional.ofNullable(request.getHeaderString(MDCOperations.HTTP_HEADER_CONSUMER_ID))
.ifPresent(MDCOperations::putConsumerId);
getHeader(request, CommonHttpHeaders.HEADER_NAV_CONSUMER_ID).ifPresent(MDCOperations::putConsumerId);
}

private static Optional<String> getHeader(ContainerRequestContext request, String header) {
return Optional.ofNullable(request.getHeaderString(header)).filter(s -> !s.isEmpty());
}

private static void setUserAndConsumerId(String subject) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package no.nav.vedtak.klient.http;

/**
* Standard NAV headere som brukes i kommunikasjon mellom Foreldrepenge-applikasjoner.
*/
public final class CommonHttpHeaders {

private CommonHttpHeaders() {
}

// Headere som brukes mellom FP-applikasjoner
public static final String HEADER_NAV_CALLID = "Nav-Callid";
public static final String HEADER_NAV_CONSUMER_ID = "Nav-Consumer-Id";

// Alternativ header - trengs for noen utgående tilfelle, trengs ikke internt
public static final String HEADER_NAV_LOWER_CALL_ID = "nav-call-id";

// Alternativ header - trengs for innkommende kall fra søknad inntil videre. Bør fjernes
public static final String HEADER_NAV_ALT_CALLID = "Nav-CallId";
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

public class BasisKontekst implements Kontekst {

private static final String APP_PSEUDO_USERID = "srv" + Optional.ofNullable(Environment.current().application()).orElse("local");
private static final String APP_CLIENTID = Optional.ofNullable(Environment.current().clientId()).orElse(APP_PSEUDO_USERID);

private final SikkerhetContext context;
private final String uid;
private final String kompaktUid;
Expand Down Expand Up @@ -52,18 +55,18 @@ public String getKonsumentId() {

// Denne brukes i prosesstask
public static BasisKontekst forProsesstaskUtenSystembruker() {
var username = "srv" + Optional.ofNullable(Environment.current().application()).orElse("local");
var konsument = Optional.ofNullable(Environment.current().clientId()).orElse(username);
return new BasisKontekst(SikkerhetContext.SYSTEM, username, IdentType.Prosess, konsument);
return new BasisKontekst(SikkerhetContext.SYSTEM, APP_PSEUDO_USERID, IdentType.Prosess, APP_CLIENTID);
}

public static BasisKontekst ikkeAutentisertRequest(String consumerId) {
var consumer = Optional.ofNullable(consumerId)
.or(() -> Optional.ofNullable(Environment.current().application()).map(a -> "srv" + a))
.orElse("srvlocal");
var consumer = Optional.ofNullable(consumerId).orElse(APP_PSEUDO_USERID);
return new BasisKontekst(SikkerhetContext.REQUEST, null, null, consumer);
}

public static String getAppConsumerId() {
return APP_CLIENTID;
}

static BasisKontekst tomKontekst() {
return new BasisKontekst(null, null, null, null);
}
Expand Down
44 changes: 15 additions & 29 deletions felles/log/src/main/java/no/nav/vedtak/log/mdc/MDCOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,22 @@
import java.util.Objects;
import java.util.Optional;

import javax.xml.namespace.QName;

import org.slf4j.MDC;

/**
* Utility-klasse for kommunikasjon med MDC.
*/
public final class MDCOperations {
public static final String HTTP_HEADER_CALL_ID = "Nav-Callid";
public static final String HTTP_HEADER_ALT_CALL_ID = "nav-call-id";
public static final String HTTP_HEADER_CONSUMER_ID = "Nav-Consumer-Id";

public static final String NAV_CALL_ID = "Nav-CallId";
public static final String NAV_USER_ID = "Nav-userId";
public static final String NAV_CONSUMER_ID = "Nav-ConsumerId";

@Deprecated
/*
* Bruk NAV_CALL_ID isteden
*/ public static final String MDC_CALL_ID = "callId";
@Deprecated(forRemoval = true) // Immediate etter tasks ferdig
private static final String NAV_CALL_ID = "Nav-CallId";
@Deprecated(forRemoval = true) // Immediate
private static final String NAV_USER_ID = "Nav-userId";
@Deprecated(forRemoval = true) // Immediate
private static final String NAV_CONSUMER_ID = "Nav-ConsumerId";

@Deprecated
/*
* Bruk NAV_USER_ID isteden
*/ public static final String MDC_USER_ID = "userId";

@Deprecated
/*
* Bruk NAV_CONSUMER_ID isteden
*/ public static final String MDC_CONSUMER_ID = "consumerId";
private static final String MDC_CALL_ID = "callId";
private static final String MDC_USER_ID = "userId";
private static final String MDC_CONSUMER_ID = "consumerId";

private MDCOperations() {
}
Expand All @@ -46,11 +32,11 @@ public static void putCallId() {
}

public static void putCallId(String callId) {
toMDC(NAV_CALL_ID, callId);
toMDC(MDC_CALL_ID, callId);
}

public static String getCallId() {
return Optional.ofNullable(get(NAV_CALL_ID)).orElse(get(MDC_CALL_ID));
return Optional.ofNullable(get(MDC_CALL_ID)).orElseGet(() -> get(NAV_CALL_ID));
}

public static void removeCallId() {
Expand All @@ -59,11 +45,11 @@ public static void removeCallId() {
}

public static void putConsumerId(String consumerId) {
toMDC(NAV_CONSUMER_ID, consumerId);
toMDC(MDC_CONSUMER_ID, consumerId);
}

public static String getConsumerId() {
return Optional.ofNullable(get(NAV_CONSUMER_ID)).orElse(get(MDC_CONSUMER_ID));
return Optional.ofNullable(get(MDC_CONSUMER_ID)).orElseGet(() -> get(NAV_CONSUMER_ID));
}

public static void removeConsumerId() {
Expand All @@ -73,11 +59,11 @@ public static void removeConsumerId() {

public static void putUserId(String userId) {
Objects.requireNonNull(userId, "userId can't be null");
put(NAV_USER_ID, maskFnr(userId));
put(MDC_USER_ID, maskFnr(userId));
}

public static String getUserId() {
return Optional.ofNullable(get(NAV_USER_ID)).orElse(MDC_USER_ID);
return Optional.ofNullable(get(MDC_USER_ID)).orElseGet(() -> get(NAV_USER_ID));
}

public static void removeUserId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,44 @@
package no.nav.vedtak.log.mdc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

import org.slf4j.MDC;

/**
* {@link MDC} backet parameter som tillater en semi-colon separert liste av
* sub-keys. Kan dermed legge til og fjerne ekstra-kontekst data dynamisk.
* {@link MDC} backed parameter med felles prefix (kan være tom)
*/
public class MdcExtendedLogContext {

private static final Pattern ILLEGAL_CHARS = Pattern.compile("[\\[\\];=]");
private final String paramName;
private final String baseFormat;
private final String prefix; // May be empty string, but not null

public MdcExtendedLogContext(String paramName) {
Objects.requireNonNull(paramName, "paramName");
this.paramName = paramName;
this.baseFormat = paramName + "[]";
public MdcExtendedLogContext(String prefix) {
Objects.requireNonNull(prefix, "paramName");
this.prefix = prefix;
}

public static MdcExtendedLogContext getContext(String kontekstParamNavn) {
return new MdcExtendedLogContext(kontekstParamNavn);
public static MdcExtendedLogContext getContext(String kontekstPrefix) {
return new MdcExtendedLogContext(kontekstPrefix);
}

public void add(String key, Object value) {
String currentValue = mdcKey();
String cleanedValue = removeKeyValue(key, currentValue);
String newValue = insertValue(cleanedValue, key, value);
MDC.put(paramName, newValue);
validateKey(key);
if (value == null) {
MDC.remove(paramKey(key));
} else {
MDC.put(paramKey(key), value.toString());
}
}

private String paramKey(String key) {
return paramName + "_" + key;
}

public String getFullText() {
return MDC.get(paramName);
public void remove(String key) {
validateKey(key);
MDC.remove(paramKey(key));
}

private String insertValue(String currentValue, String key, Object keyValue) {
public String get(String key) {
validateKey(key);
if (currentValue == null) {
currentValue = this.baseFormat;
}
String val = currentValue.substring(0, currentValue.length() - 1);
val = val + (val.endsWith("[") ? "" : ";") + (keyValue == null ? "" : key + "=" + keyValue) + "]";
return val;
return MDC.get(paramKey(key));
}

private static void validateKey(String key) {
Expand All @@ -65,50 +47,8 @@ private static void validateKey(String key) {
}
}

public void remove(String key) {
validateKey(key);
MDC.remove(paramKey(key));
String currentValue = mdcKey();
if (currentValue == null) {
return;
}
String newValue = removeKeyValue(key, currentValue);
MDC.put(paramName, newValue);
}

private String mdcKey() {
String currentValue = MDC.get(paramName);
return (currentValue != null) ? currentValue : baseFormat;
}

private String removeKeyValue(String key, String orgValue) {
if (orgValue == null) {
return null;
}

List<String> contentList = splitParts(orgValue);

int orgSize = contentList.size();
for (int i = orgSize; --i >= 0; ) {
if (contentList.get(i).startsWith(key + "=")) {
contentList.remove(i);
}
}
if (orgSize == contentList.size()) {
return orgValue;
} else if (contentList.isEmpty()) {
return null;
} else {
return paramName + "[" + String.join(";", contentList) + "]";
}

private String paramKey(String key) {
return prefix + "_" + key;
}

private List<String> splitParts(String orgValue) {
if (orgValue == null) {
return Collections.emptyList();
}
String content = orgValue.substring(paramName.length() + 1, orgValue.length() - 1);
return new ArrayList<>(Arrays.asList(content.split(";")));
}
}
Loading

0 comments on commit 22e6331

Please sign in to comment.