diff --git a/testing/indexer-test-azure/pom.xml b/testing/indexer-test-azure/pom.xml index 7d96ec79c242458691258ce53923882c3556a2f6..26ee0981f960dfb7d5c787b3e205bed7da151abd 100644 --- a/testing/indexer-test-azure/pom.xml +++ b/testing/indexer-test-azure/pom.xml @@ -177,6 +177,28 @@ <version>3.0.0</version> <scope>test</scope> </dependency> - +<!--Added dependency for utilizing azure service principle class--> + <dependency> + <groupId>org.opengroup.osdu</groupId> + <artifactId>core-lib-azure</artifactId> + <version>0.0.28</version> + <scope>test</scope> + <exclusions> + <exclusion> + <groupId>com.azure</groupId> + <artifactId>azure-core-http-netty</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>io.netty</groupId> + <artifactId>netty-all</artifactId> + <version>4.1.50.Final</version> + </dependency> + <dependency> + <groupId>io.netty</groupId> + <artifactId>netty-transport</artifactId> + <version>4.1.15.Final</version> + </dependency> </dependencies> </project> \ No newline at end of file diff --git a/testing/indexer-test-azure/src/test/java/org/opengroup/osdu/util/AzureServicePrincipal.java b/testing/indexer-test-azure/src/test/java/org/opengroup/osdu/util/AzureServicePrincipal.java deleted file mode 100644 index 57ef5389cbf43c415dd5d75e26d6411ad5a1f117..0000000000000000000000000000000000000000 --- a/testing/indexer-test-azure/src/test/java/org/opengroup/osdu/util/AzureServicePrincipal.java +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright © Microsoft Corporation -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package org.opengroup.osdu.util; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; - -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLEncoder; -import java.util.HashMap; -import java.util.Map; - -public class AzureServicePrincipal { - public static String getIdToken(String sp_id, String sp_secret, String tenant_id, String app_resource_id) throws Exception { - String aad_endpoint = String.format("https://login.microsoftonline.com/%s/oauth2/token", tenant_id); - URL url = new URL(aad_endpoint); - HttpURLConnection con = (HttpURLConnection) url.openConnection(); - con.setRequestMethod("POST"); - con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - - Map<String, String> parameters = new HashMap<>(); - parameters.put("grant_type", "client_credentials"); - parameters.put("client_id", sp_id); - parameters.put("client_secret", sp_secret); - parameters.put("resource", app_resource_id); - - con.setDoOutput(true); - DataOutputStream out = new DataOutputStream(con.getOutputStream()); - out.writeBytes(getParamsString(parameters)); - out.flush(); - out.close(); - - BufferedReader in = new BufferedReader( - new InputStreamReader(con.getInputStream())); - String inputLine; - StringBuffer content = new StringBuffer(); - while ((inputLine = in.readLine()) != null) { - content.append(inputLine); - } - in.close(); - - con.disconnect(); - - Gson gson = new Gson(); - JsonObject jobj = gson.fromJson(content.toString(), JsonObject.class); - String token = jobj.get("access_token").getAsString(); - return token; - } - - private static String getParamsString(Map<String, String> params) - throws UnsupportedEncodingException { - StringBuilder result = new StringBuilder(); - - for (Map.Entry<String, String> entry : params.entrySet()) { - result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); - result.append("="); - result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); - result.append("&"); - } - - String resultString = result.toString(); - return resultString.length() > 0 - ? resultString.substring(0, resultString.length() - 1) - : resultString; - } -} \ No newline at end of file diff --git a/testing/indexer-test-azure/src/test/java/org/opengroup/osdu/util/JwtTokenUtil.java b/testing/indexer-test-azure/src/test/java/org/opengroup/osdu/util/JwtTokenUtil.java index b1a799920c452c1f5096a146e7129ade22d22560..355013c9710b2a7bcd571f5fd2b756ac967fcbe2 100644 --- a/testing/indexer-test-azure/src/test/java/org/opengroup/osdu/util/JwtTokenUtil.java +++ b/testing/indexer-test-azure/src/test/java/org/opengroup/osdu/util/JwtTokenUtil.java @@ -14,6 +14,8 @@ package org.opengroup.osdu.util; +import org.opengroup.osdu.azure.util.AzureServicePrincipal; + class JwtTokenUtil { static public String getAccessToken() throws Exception { @@ -23,7 +25,7 @@ class JwtTokenUtil { String sp_secret = System.getProperty("AZURE_TESTER_SERVICEPRINCIPAL_SECRET", System.getenv("AZURE_TESTER_SERVICEPRINCIPAL_SECRET")); String tenant_id = System.getProperty("AZURE_AD_TENANT_ID", System.getenv("AZURE_AD_TENANT_ID")); String app_resource_id = System.getProperty("AZURE_AD_APP_RESOURCE_ID", System.getenv("AZURE_AD_APP_RESOURCE_ID")); - token = AzureServicePrincipal.getIdToken(sp_id, sp_secret, tenant_id, app_resource_id); + token = new AzureServicePrincipal().getIdToken(sp_id, sp_secret, tenant_id, app_resource_id); return token; }