Skip to content

Commit

Permalink
Hindrer logging av FNR når Nav-Consumer-Id ikke er satt. (#1392)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsladek authored Nov 13, 2024
1 parent 7f27367 commit 598fd92
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
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.log.mdc.FnrUtils;
import no.nav.vedtak.log.mdc.MDCOperations;
import no.nav.vedtak.sikkerhet.kontekst.BasisKontekst;
import no.nav.vedtak.sikkerhet.kontekst.KontekstHolder;
Expand Down Expand Up @@ -101,7 +102,7 @@ private static void setCallAndConsumerId(ContainerRequestContext request) {
private static void setUserAndConsumerId(String subject) {
Optional.ofNullable(subject).ifPresent(MDCOperations::putUserId);
if (MDCOperations.getConsumerId() == null && subject != null) {
MDCOperations.putConsumerId(subject);
MDCOperations.putConsumerId(FnrUtils.maskFnr(subject));
}
}

Expand Down
14 changes: 14 additions & 0 deletions felles/log/src/main/java/no/nav/vedtak/log/mdc/FnrUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package no.nav.vedtak.log.mdc;

public class FnrUtils {
private FnrUtils() {
// hide construktor
}

public static String maskFnr(String userId) {
if (userId.matches("^\\d{11}$")) {
return userId.replaceAll("\\d{5}$", "*****");
}
return userId;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.nav.vedtak.log.mdc;

import static no.nav.vedtak.log.mdc.FnrUtils.maskFnr;
import static org.slf4j.MDC.get;
import static org.slf4j.MDC.put;

Expand Down Expand Up @@ -37,9 +38,6 @@ public final class MDCOperations {
* Bruk NAV_CONSUMER_ID isteden
*/ public static final String MDC_CONSUMER_ID = "consumerId";

// QName for the callId header
public static final QName CALLID_QNAME = new QName("uri:no.nav.applikasjonsrammeverk", MDC_CALL_ID);

private MDCOperations() {
}

Expand Down Expand Up @@ -78,13 +76,6 @@ public static void putUserId(String userId) {
put(NAV_USER_ID, maskFnr(userId));
}

private static String maskFnr(String userId) {
if (userId.matches("^\\d{11}$")) {
return userId.replaceAll("\\d{5}$", "*****");
}
return userId;
}

public static String getUserId() {
return Optional.ofNullable(get(NAV_USER_ID)).orElse(MDC_USER_ID);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ public static String padEnd(String string, int minLength, char padChar) {
if (string.length() >= minLength) {
return string;
}
StringBuilder sb = new StringBuilder(minLength);
sb.append(string);
for (int i = string.length(); i < minLength; i++) {
sb.append(padChar);
}
return sb.toString();
return string + String.valueOf(padChar).repeat(minLength - string.length());
}
}
31 changes: 31 additions & 0 deletions felles/log/src/test/java/no/nav/vedtak/log/mdc/FnrUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package no.nav.vedtak.log.mdc;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class FnrUtilsTest {

@Test
void maskerFnrTest() {
var fdato = "122314";
var pnr = "55555";
var fakeTestFnr = fdato + pnr;
var maskertFnr = FnrUtils.maskFnr(fakeTestFnr);
assertThat(maskertFnr).isNotBlank().startsWith(fdato).doesNotContain(pnr).endsWith("*****");
}

@Test
void ikkeMaskerVanligTekst() {
var ikkeFnr = "dev-fss:teamforeldrepenger:fpsak";
var ikkeMaskert = FnrUtils.maskFnr(ikkeFnr);
assertThat(ikkeMaskert).isEqualTo(ikkeFnr);
}

@Test
void ikkeMaskerOrgnr() {
var fakeOrgnr9siffer = "123456789";
var ikkeMaskert = FnrUtils.maskFnr(fakeOrgnr9siffer);
assertThat(ikkeMaskert).isEqualTo(fakeOrgnr9siffer);
}
}

0 comments on commit 598fd92

Please sign in to comment.