Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Open Subsurface Data Universe Software
Platform
Data Flow
Data Enrichment
wks
Commits
48930036
Commit
48930036
authored
Aug 30, 2021
by
Abhishek Patil
Committed by
harshit aggarwal
Aug 30, 2021
Browse files
Using AzureServicePrincipleTokenService in implementation of UserCredential
parent
a4574ae1
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
NOTICE
View file @
48930036
This diff is collapsed.
Click to expand it.
provider/wks-azure/pom.xml
View file @
48930036
...
...
@@ -12,7 +12,7 @@
<description>
WKS service on Azure
</description>
<packaging>
jar
</packaging>
<properties>
<osdu.corelibazure.version>
0.11.0-rc
4
</osdu.corelibazure.version>
<osdu.corelibazure.version>
0.11.0-rc
5
</osdu.corelibazure.version>
<azure.applicationinsights.version>
2.6.1
</azure.applicationinsights.version>
<osdu.os-wks-core.version>
0.11.0-SNAPSHOT
</osdu.os-wks-core.version>
<azure.version>
2.1.7
</azure.version>
...
...
provider/wks-azure/src/main/java/org/opengroup/osdu/wks/provider/azure/credentials/JwtTokenGenerator.java
View file @
48930036
package
org.opengroup.osdu.wks.provider.azure.credentials
;
import
org.opengroup.osdu.azure.util.AzureServicePrincip
a
l
;
import
org.opengroup.osdu.azure.util.AzureServicePrincipl
eTokenService
;
import
org.opengroup.osdu.core.common.model.tenant.TenantInfo
;
import
org.opengroup.osdu.wks.exceptions.ApplicationException
;
import
org.opengroup.osdu.wks.provider.azure.di.AzureBootstrapConfig
;
import
org.opengroup.osdu.wks.provider.interfaces.UserCredential
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Configuration
;
import
java.io.UnsupportedEncodingException
;
@Configuration
public
class
JwtTokenGenerator
implements
UserCredential
{
private
final
static
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
JwtTokenGenerator
.
class
);
private
static
final
String
BEARER
=
"Bearer "
;
@Autowired
private
AzureBootstrapConfig
azureBootstrapConfig
;
@Autowired
private
AzureServicePrincip
al
azureServicePrincipal
;
private
AzureServicePrincip
leTokenService
tokenService
;
@Override
public
String
getIdToken
(
TenantInfo
tenant
)
throws
ApplicationException
{
// Azure service principal will have access to all data partitions
String
token
=
null
;
try
{
token
=
BEARER
+
azureServicePrincipal
.
getIdToken
(
azureBootstrapConfig
.
getClientId
(),
azureBootstrapConfig
.
getClientSecret
(),
azureBootstrapConfig
.
getTenantId
(),
azureBootstrapConfig
.
getAppResourceId
());
}
catch
(
UnsupportedEncodingException
e
)
{
LOGGER
.
warn
(
e
.
getMessage
(),
e
);
throw
new
ApplicationException
(
e
.
getMessage
(),
e
.
getCause
());
}
return
token
;
public
String
getIdToken
(
TenantInfo
tenantInfo
){
return
"Bearer "
+
this
.
tokenService
.
getAuthorizationToken
();
}
}
provider/wks-azure/src/test/java/org/opengroup/osdu/wks/provider/azure/credentials/JwtTokenGeneratorTest.java
View file @
48930036
package
org.opengroup.osdu.wks.provider.azure.credentials
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.mockito.InjectMocks
;
import
org.mockito.Mock
;
import
org.mockito.junit.jupiter.MockitoExtension
;
import
org.opengroup.osdu.azure.util.AzureServicePrincipal
;
import
org.opengroup.osdu.azure.util.AzureServicePrincipleTokenService
;
import
org.opengroup.osdu.core.common.model.http.AppException
;
import
org.opengroup.osdu.core.common.model.tenant.TenantInfo
;
import
org.opengroup.osdu.wks.exceptions.ApplicationException
;
import
org.opengroup.osdu.wks.provider.azure.di.AzureBootstrapConfig
;
import
java.io.UnsupportedEncodingException
;
...
...
@@ -25,58 +24,34 @@ import static org.mockito.Mockito.when;
public
class
JwtTokenGeneratorTest
{
private
static
final
String
token
=
"jwt-token"
;
private
static
final
String
clientId
=
"client-id"
;
private
static
final
String
clientSecret
=
"client-secret"
;
private
static
final
String
tenantID
=
"tenant-id"
;
private
static
final
String
appResourceId
=
"app-resource-id"
;
@InjectMocks
private
JwtTokenGenerator
jwtTokenGenerator
;
@Mock
private
AzureServicePrincipal
azureServicePrincipal
;
@Mock
private
AzureBootstrapConfig
azureBootstrapConfig
;
@BeforeEach
public
void
init
()
{
when
(
azureBootstrapConfig
.
getClientId
()).
thenReturn
(
clientId
);
when
(
azureBootstrapConfig
.
getClientSecret
()).
thenReturn
(
clientSecret
);
when
(
azureBootstrapConfig
.
getTenantId
()).
thenReturn
(
tenantID
);
when
(
azureBootstrapConfig
.
getAppResourceId
()).
thenReturn
(
appResourceId
);
}
private
AzureServicePrincipleTokenService
azureServicePrincipleTokenService
;
@Test
public
void
shouldSuccessfullyGenerateToken
()
throws
UnsupportedEncodingException
,
ApplicationException
{
when
(
azureServicePrincip
al
.
getIdToken
(
clientId
,
clientSecret
,
tenantID
,
appResourceId
)).
thenReturn
(
token
);
when
(
azureServicePrincip
leTokenService
.
getAuthorizationToken
(
)).
thenReturn
(
token
);
String
result
=
jwtTokenGenerator
.
getIdToken
(
new
TenantInfo
());
assertEquals
(
"Bearer "
+
token
,
result
);
verify
(
azureServicePrincipal
,
times
(
1
)).
getIdToken
(
clientId
,
clientSecret
,
tenantID
,
appResourceId
);
verify
(
azureBootstrapConfig
,
times
(
1
)).
getClientId
();
verify
(
azureBootstrapConfig
,
times
(
1
)).
getClientSecret
();
verify
(
azureBootstrapConfig
,
times
(
1
)).
getTenantId
();
verify
(
azureBootstrapConfig
,
times
(
1
)).
getAppResourceId
();
verify
(
azureServicePrincipleTokenService
,
times
(
1
)).
getAuthorizationToken
();
}
@Test
public
void
shouldThrowApp
lication
Exception
()
throws
UnsupportedEncodingException
{
public
void
shouldThrowAppException
()
throws
UnsupportedEncodingException
{
doThrow
(
UnsupportedEncoding
Exception
.
class
).
when
(
azureServicePrincip
al
).
getIdToken
(
clientId
,
clientSecret
,
tenantID
,
appResourceId
);
doThrow
(
App
Exception
.
class
).
when
(
azureServicePrincip
leTokenService
).
getAuthorizationToken
(
);
App
lication
Exception
exception
=
assertThrows
(
App
lication
Exception
.
class
,
()
->
{
AppException
exception
=
assertThrows
(
AppException
.
class
,
()
->
{
jwtTokenGenerator
.
getIdToken
(
new
TenantInfo
());
});
assertNotNull
(
exception
);
verify
(
azureServicePrincipal
,
times
(
1
)).
getIdToken
(
clientId
,
clientSecret
,
tenantID
,
appResourceId
);
verify
(
azureBootstrapConfig
,
times
(
1
)).
getClientId
();
verify
(
azureBootstrapConfig
,
times
(
1
)).
getClientSecret
();
verify
(
azureBootstrapConfig
,
times
(
1
)).
getTenantId
();
verify
(
azureBootstrapConfig
,
times
(
1
)).
getAppResourceId
();
verify
(
azureServicePrincipleTokenService
,
times
(
1
)).
getAuthorizationToken
();
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment