3535import com .github .nagyesta .lowkeyvault .http .AuthorityOverrideFunction ;
3636import com .github .nagyesta .lowkeyvault .http .management .LowkeyVaultException ;
3737import com .github .nagyesta .lowkeyvault .testcontainers .LowkeyVaultContainer ;
38- import com .github .tomakehurst .wiremock .WireMockServer ;
39- import com .github .tomakehurst .wiremock .client .WireMock ;
4038
4139import io .kroxylicious .kms .provider .azure .AzureKeyVaultEdek ;
4240import io .kroxylicious .kms .provider .azure .AzureKeyVaultKmsService ;
5048import io .kroxylicious .proxy .config .secret .InlinePassword ;
5149import io .kroxylicious .proxy .config .tls .Tls ;
5250import io .kroxylicious .proxy .config .tls .TrustStore ;
53- import io .kroxylicious .testing .kafka .common .KeytoolCertificateGenerator ;
5451
5552import edu .umd .cs .findbugs .annotations .Nullable ;
5653
5754import static com .github .nagyesta .lowkeyvault .testcontainers .LowkeyVaultContainerBuilder .lowkeyVault ;
58- import static com .github .tomakehurst .wiremock .client .WireMock .post ;
59- import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .wireMockConfig ;
6055
6156@ SuppressWarnings ("java:S112" )
6257public class AzureKeyVaultKmsTestKmsFacade implements TestKmsFacade <AzureKeyVaultConfig , WrappingKey , AzureKeyVaultEdek > {
6358
64- public static final String MOCK_AUTH_RESPONSE = """
65- {
66- "access_token": "aaa",
67- "token_type": "Bearer",
68- "expires_in": 3599,
69- "scope": "https%3A%2F%2Fgraph.microsoft.com%2Fmail.read",
70- "refresh_token": "bbb",
71- "id_token": "ccc"
72- }
73- """ ;
7459 public static final String TENANT_ID = "identity" ;
7560 public static final String KEY_VAULT_NAME = "default" ;
7661
77- private final KeytoolCertificateGenerator entraCertGen = entraCerts ();
7862 @ Nullable
7963 private LowkeyVaultContainer kms ;
8064 @ Nullable
81- private WireMockServer entraMock ;
65+ private OauthServerContainer oauthServer ;
8266
8367 protected AzureKeyVaultKmsTestKmsFacade () {
8468 }
@@ -89,36 +73,26 @@ public boolean isAvailable() {
8973 }
9074
9175 public void startKms () {
92- this .kms = startVault ();
93- this .entraMock = new WireMockServer (wireMockConfig ().dynamicPort ().keystorePath (entraCertGen .getKeyStoreLocation ()).keystorePassword (entraCertGen .getPassword ()));
94- entraMock .start ();
95- entraMock .stubFor (post ("/" + TENANT_ID + "/oauth2/v2.0/token" )
96- .willReturn (WireMock .aResponse ().withStatus (200 ).withHeader ("Content-type" , "application/json" ).withBody (MOCK_AUTH_RESPONSE )));
97- entraMock .start ();
76+ this .kms = startKeyVault ();
77+ this .oauthServer = startMockOauthServer ();
9878 }
9979
100- private static KeytoolCertificateGenerator entraCerts () {
101- try {
102- KeytoolCertificateGenerator entraCertGen = new KeytoolCertificateGenerator ();
103- entraCertGen .
generateSelfSignedCertificateEntry (
"[email protected] " ,
"example.com" ,
"Engineering" ,
"kroxylicious.io" ,
null ,
null ,
"NZ" );
104- entraCertGen .generateTrustStore (entraCertGen .getCertFilePath (), "website" );
105- return entraCertGen ;
106- }
107- catch (Exception e ) {
108- throw new TestKmsFacadeException (e );
109- }
80+ private static OauthServerContainer startMockOauthServer () {
81+ OauthServerContainer oauthServerContainer = new OauthServerContainer ();
82+ oauthServerContainer .start ();
83+ return oauthServerContainer ;
11084 }
11185
11286 public void stopKms () {
11387 if (kms != null ) {
11488 kms .stop ();
11589 }
116- if (entraMock != null ) {
117- entraMock .stop ();
90+ if (oauthServer != null ) {
91+ oauthServer .stop ();
11892 }
11993 }
12094
121- public LowkeyVaultContainer startVault () {
95+ public static LowkeyVaultContainer startKeyVault () {
12296 String image = "nagyesta/lowkey-vault:4.0.0" ;
12397 final DockerImageName imageName = DockerImageName .parse ("mirror.gcr.io/" + image )
12498 .asCompatibleSubstituteFor (DockerImageName .parse (image ));
@@ -141,7 +115,7 @@ public AzureKeyVaultConfig getKmsServiceConfig() {
141115 if (kms == null ) {
142116 throw new IllegalStateException ("kms is not initialized" );
143117 }
144- if (entraMock == null ) {
118+ if (oauthServer == null ) {
145119 throw new IllegalStateException ("entraMock is not initialized" );
146120 }
147121 URI defaultVaultBaseUrl = URI .create (kms .getDefaultVaultBaseUrl ());
@@ -153,10 +127,12 @@ public AzureKeyVaultConfig getKmsServiceConfig() {
153127 }
154128 TrustStore vaultTrust = new TrustStore (tempFile .getAbsolutePath (), new InlinePassword (kms .getDefaultKeyStorePassword ()), defaultKeyStore .getType ());
155129 Tls vaultTls = new Tls (null , vaultTrust , null , null );
156- TrustStore entraTrust = new TrustStore (entraCertGen .getTrustStoreLocation (), new InlinePassword (entraCertGen .getPassword ()), defaultKeyStore .getType ());
130+ TrustStore entraTrust = new TrustStore (oauthServer .getTrustStoreLocation (), new InlinePassword (oauthServer .getTrustStorePassword ()),
131+ oauthServer .getTrustStoreType ());
157132 Tls entraTls = new Tls (null , entraTrust , null , null );
158133 return new AzureKeyVaultConfig (
159- new EntraIdentityConfig (URI .create (entraMock .baseUrl ()), TENANT_ID , new InlinePassword ("abc" ), new InlinePassword ("def" ), null , entraTls ),
134+ new EntraIdentityConfig (oauthServer .getBaseUri (), TENANT_ID , new InlinePassword ("abc" ), new InlinePassword ("def" ), null ,
135+ entraTls ),
160136 KEY_VAULT_NAME , defaultVaultBaseUrl .getHost (), null , defaultVaultBaseUrl .getPort (), vaultTls );
161137 }
162138 catch (Exception e ) {
@@ -257,4 +233,5 @@ private static HttpClient createHttpClient(String clientAuthority, String contai
257233 }
258234 }
259235 }
236+
260237}
0 commit comments