11package em .external .familie .ba .sak ;
22
3+ import com .nimbusds .jose .JOSEObjectType ;
4+ import no .nav .security .mock .oauth2 .MockOAuth2Server ;
5+ import no .nav .security .mock .oauth2 .OAuth2Config ;
6+ import no .nav .security .mock .oauth2 .token .RequestMapping ;
7+ import no .nav .security .mock .oauth2 .token .RequestMappingTokenCallback ;
38import org .evomaster .client .java .controller .ExternalSutController ;
49import org .evomaster .client .java .controller .InstrumentedSutStarter ;
510import org .evomaster .client .java .controller .api .dto .auth .AuthenticationDto ;
611import org .evomaster .client .java .controller .api .dto .SutInfoDto ;
12+ import org .evomaster .client .java .controller .api .dto .auth .HttpVerb ;
13+ import org .evomaster .client .java .controller .api .dto .auth .LoginEndpointDto ;
14+ import org .evomaster .client .java .controller .api .dto .auth .TokenHandlingDto ;
715import org .evomaster .client .java .controller .api .dto .database .schema .DatabaseType ;
816import org .evomaster .client .java .controller .problem .RestProblem ;
917import org .evomaster .client .java .sql .DbCleaner ;
1725import java .sql .Connection ;
1826import java .sql .DriverManager ;
1927import java .sql .SQLException ;
20- import java .util .Arrays ;
21- import java .util .Collections ;
22- import java .util .List ;
28+ import java .util .*;
2329
2430public class ExternalEvoMasterController extends ExternalSutController {
2531
@@ -81,6 +87,36 @@ public static void main(String[] args) {
8187 .withTmpFs (Collections .singletonMap ("/var/lib/postgresql/data" , "rw" ))
8288 .withExposedPorts (POSTGRES_PORT );
8389
90+ private MockOAuth2Server oAuth2Server ;
91+
92+ private int oAuth2Port ;
93+
94+ private final String ISSUER_ID = "azuread" ;
95+
96+ private final String DEFAULT_AUDIENCE = "some-audience" ;
97+
98+ private final String PROSESSERING_ROLLE = "928636f4-fd0d-4149-978e-a6fb68bb19de" ;
99+
100+ private final String TOKEN_PARAM = "name" ;
101+
102+ private static final String A0 = "TaskRunner" ;
103+ private static final String A1 = "Veileder" ;
104+ private static final String A2 = "Saksbehandler" ;
105+ private static final String A3 = "Beslutter" ;
106+ private static final String A4 = "Forvalter" ;
107+ private static final String A5 = "Kode6" ;
108+ private static final String A6 = "Kode7" ;
109+ private static final String A7 = "System" ;
110+
111+ private static final String veileder = "93a26831-9866-4410-927b-74ff51a9107c" ;
112+ private static final String saksbehandler = "d21e00a4-969d-4b28-8782-dc818abfae65" ;
113+ private static final String beslutter = "9449c153-5a1e-44a7-84c6-7cc7a8867233" ;
114+ private static final String forvalter = "c62e908a-cf20-4ad0-b7b3-3ff6ca4bf38b" ;
115+ private static final String kode6 = "5ef775f2-61f8-4283-bf3d-8d03f428aa14" ;
116+ private static final String kode7 = "ea930b6b-9397-44d9-b9e6-f4cf527a632a" ;
117+
118+
119+
84120 public ExternalEvoMasterController (){
85121 this (40100 , "../core/target" , 12345 , 120 , "java" );
86122 }
@@ -100,6 +136,7 @@ public ExternalEvoMasterController(
100136
101137
102138 this .sutPort = sutPort ;
139+ this .oAuth2Port = sutPort + 1 ;
103140 this .jarLocation = jarLocation ;
104141 this .timeoutSeconds = timeoutSeconds ;
105142 setControllerPort (controllerPort );
@@ -109,6 +146,9 @@ public ExternalEvoMasterController(
109146
110147 @ Override
111148 public String [] getInputParameters () {
149+
150+ String wellKnownUrl = oAuth2Server .wellKnownUrl (ISSUER_ID ).toString ();
151+
112152 return new String []{
113153 "--server.port=" + sutPort ,
114154 "--spring.profiles.active=dev" ,
@@ -125,6 +165,25 @@ public String[] getInputParameters() {
125165 "--logging.level.root=OFF" ,
126166 "--logging.config=classpath:logback-spring.xml" ,
127167 "--logging.level.org.springframework=INFO" ,
168+ "--no.nav.security.jwt.issuer.azuread.discoveryurl=" +wellKnownUrl ,
169+ "--prosessering.rolle=" + PROSESSERING_ROLLE ,
170+ "--FAMILIE_EF_SAK_API_URL=http://fake-familie-ef-sak/api" ,
171+ "--FAMILIE_KLAGE_URL=http://fake-familie-klage" ,
172+ "--FAMILIE_BREV_API_URL=http://fake-familie-brev" ,
173+ "--FAMILIE_BA_INFOTRYGD_FEED_API_URL=http://fake-familie-ba-infotrygd-feed/api" ,
174+ "--FAMILIE_BA_INFOTRYGD_API_URL=http://fake-familie-ba-infotrygd" ,
175+ "--FAMILIE_TILBAKE_API_URL=http://fake-familie-tilbake/api" ,
176+ "--PDL_URL=http://fake-pdl-api.default" ,
177+ "--FAMILIE_INTEGRASJONER_API_URL=http://fake-familie-integrasjoner/api" ,
178+ "--FAMILIE_OPPDRAG_API_URL=http://fake-familie-oppdrag/api" ,
179+ "--SANITY_FAMILIE_API_URL=http://fake-xsrv1mh6.apicdn.sanity.io/v2021-06-07/data/query/ba-brev" ,
180+ "--ECB_API_URL=http://fake-data-api.ecb.europa.eu/service/data/EXR/" ,
181+ "--rolle.veileder=" + veileder ,
182+ "--rolle.saksbehandler=" + saksbehandler ,
183+ "--rolle.beslutter=" + beslutter ,
184+ "--rolle.forvalter=" + forvalter ,
185+ "--rolle.kode6=" + kode6 ,
186+ "--rolle.kode7=" + kode7
128187 };
129188 }
130189
@@ -136,7 +195,8 @@ public String[] getJVMParameters() {
136195 "-DAZURE_APP_CLIENT_ID=bar" ,
137196 "-DNAIS_APP_NAME=bar" ,
138197 "-DUNLEASH_SERVER_API_URL=http://fake-unleash-server-api.no:8080" ,
139- "-DUNLEASH_SERVER_API_TOKEN=bar"
198+ "-DUNLEASH_SERVER_API_TOKEN=bar" ,
199+ "-DBA_SAK_CLIENT_ID=" +DEFAULT_AUDIENCE
140200 };
141201 }
142202
@@ -172,6 +232,8 @@ public long getMaxAwaitForInitializationInSeconds() {
172232 @ Override
173233 public void preStart () {
174234 postgres .start ();
235+ oAuth2Server = new MockOAuth2Server (getOAuth2Config ());
236+ oAuth2Server .start (oAuth2Port );
175237 }
176238
177239 @ Override
@@ -198,6 +260,7 @@ public void preStop() {
198260 @ Override
199261 public void postStop () {
200262 postgres .stop ();
263+ if (oAuth2Server !=null ) oAuth2Server .shutdown ();
201264 }
202265
203266 private void closeDataBaseConnection () {
@@ -232,9 +295,92 @@ public SutInfoDto.OutputFormat getPreferredOutputFormat() {
232295
233296 @ Override
234297 public List <AuthenticationDto > getInfoForAuthentication () {
235- return null ;
298+
299+ String url = oAuth2Server .baseUrl () + ISSUER_ID + "/token" ;
300+
301+ return Arrays .asList (
302+ getAuthenticationDto (A0 ,url ),
303+ getAuthenticationDto (A1 ,url ),
304+ getAuthenticationDto (A2 ,url ),
305+ getAuthenticationDto (A3 ,url ),
306+ getAuthenticationDto (A4 ,url ),
307+ getAuthenticationDto (A5 ,url ),
308+ getAuthenticationDto (A6 ,url ),
309+ getAuthenticationDto (A7 ,url )
310+ );
311+ }
312+
313+ private RequestMapping getRequestMapping (String label , List <String > groups , String id , String name ) {
314+ Map <String ,Object > claims = new HashMap <>();
315+ claims .put ("groups" ,groups );
316+ claims .put ("name" ,name );
317+ claims .put ("NAVident" , id );
318+ claims .put ("sub" ,"subject" );
319+ claims .put ("aud" ,"some-audience" );
320+ claims .put ("tid" ,ISSUER_ID );
321+ claims .put ("azp" ,id );
322+
323+ RequestMapping rm = new RequestMapping (TOKEN_PARAM ,label ,claims , JOSEObjectType .JWT .getType ());
324+
325+ return rm ;
236326 }
237327
328+ private OAuth2Config getOAuth2Config (){
329+
330+ List <RequestMapping > mappings = Arrays .asList ( getRequestMapping (A0 , Arrays .asList (PROSESSERING_ROLLE ),"Z0042" , "Task Runner" ),
331+ getRequestMapping (A1 , Arrays .asList (veileder ),"Z0000" , "Mock McMockface" ),
332+ getRequestMapping (A2 , Arrays .asList (saksbehandler ),"Z0001" , "Foo Bar" ),
333+ getRequestMapping (A3 , Arrays .asList (beslutter ),"Z0002" , "John Smith" ),
334+ getRequestMapping (A4 , Arrays .asList (forvalter ),"Z0003" , "Mario Rossi" ),
335+ getRequestMapping (A5 , Arrays .asList (kode6 ),"Z0004" , "Kode Six" ),
336+ getRequestMapping (A6 , Arrays .asList (kode7 ),"Z0005" , "Kode Seven" ),
337+ getRequestMapping (A7 , Arrays .asList (),"VL" , "The System" )
338+ );
339+
340+ RequestMappingTokenCallback callback = new RequestMappingTokenCallback (
341+ ISSUER_ID ,
342+ mappings ,
343+ 360000
344+ );
345+
346+ Set <RequestMappingTokenCallback > callbacks = Set .of (
347+ callback
348+ );
349+
350+ OAuth2Config config = new OAuth2Config (
351+ true ,
352+ null ,
353+ null ,
354+ false ,
355+ new no .nav .security .mock .oauth2 .token .OAuth2TokenProvider (),
356+ callbacks
357+ );
358+
359+ return config ;
360+ }
361+
362+ private AuthenticationDto getAuthenticationDto (String label , String oauth2Url ){
363+
364+ AuthenticationDto dto = new AuthenticationDto (label );
365+ LoginEndpointDto x = new LoginEndpointDto ();
366+ dto .loginEndpointAuth = x ;
367+
368+ x .externalEndpointURL = oauth2Url ;
369+ x .payloadRaw = TOKEN_PARAM +"=" +label +"&grant_type=client_credentials&code=foo&client_id=foo&client_secret=secret" ;
370+ x .verb = HttpVerb .POST ;
371+ x .contentType = "application/x-www-form-urlencoded" ;
372+ x .expectCookies = false ;
373+
374+ TokenHandlingDto token = new TokenHandlingDto ();
375+ token .headerPrefix = "Bearer " ;
376+ token .httpHeaderName = "Authorization" ;
377+ token .extractFromField = "/access_token" ;
378+ x .token = token ;
379+
380+ return dto ;
381+ }
382+
383+
238384 @ Override
239385 public List <DbSpecification > getDbSpecifications () {
240386 return dbSpecification ;
0 commit comments