-
Notifications
You must be signed in to change notification settings - Fork 2.2k
light weight Msi auth helper library to be used by SQL #2755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3d4dce5
code for a simple utility to get an MSI token
praries880 1cd358d
Fix error handelling
praries880 a45d8a2
fix pom file and logic in app
praries880 d8ebb84
fix bugs and error cases
praries880 ec738d0
Add readme with instructions on how to use the library
praries880 ff27a52
Rename files to remove "helper" from the anme...
praries880 9eca891
update the cred provider to be inline with what the sql team will
praries880 5d14dcf
Add property required by SQL
praries880 5058759
Merge branch 'master' of https://github.com/azure/azure-sdk-for-java …
praries880 985f38f
Took care of review comments
praries880 40d3d1a
Merge branch 'master' of https://github.com/azure/azure-sdk-for-java …
praries880 db4b534
update the readme
praries880 c8f8024
Update version
praries880 b0c474c
Merge branch 'master' of https://github.com/azure/azure-sdk-for-java …
praries880 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| <!-- | ||
| Copyright (c) Microsoft Corporation. All rights reserved. | ||
| Licensed under the MIT License. See License.txt in the project root for | ||
| license information. | ||
| --> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <groupId>com.microsoft.azure.msi_auth_token_provider</groupId> | ||
|
|
||
| <artifactId>azure-authentication-msi-token-provider</artifactId> | ||
| <packaging>jar</packaging> | ||
| <version>1.0.0-beta</version> | ||
|
|
||
| <name>Azure Java Client MSI Authorization Token Provoider Library</name> | ||
| <description>This package contains the MSI token provider classes for Azure.</description> | ||
| <url>https://github.com/Azure/azure-sdk-for-java</url> | ||
|
|
||
| <licenses> | ||
| <license> | ||
| <name>The MIT License (MIT)</name> | ||
| <url>http://opensource.org/licenses/MIT</url> | ||
| <distribution>repo</distribution> | ||
| </license> | ||
| </licenses> | ||
|
|
||
| <scm> | ||
| <url>scm:git:https://github.com/Azure/azure-sdk-for-java</url> | ||
| <connection>scm:git:git@github.com:Azure/azure-sdk-for-java.git</connection> | ||
| <tag>HEAD</tag> | ||
| </scm> | ||
|
|
||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <legal><![CDATA[[INFO] Any downloads listed may be third party software. Microsoft grants you no rights for third party software.]]></legal> | ||
| </properties> | ||
|
|
||
| <developers> | ||
| <developer> | ||
| <id>microsoft</id> | ||
| <name>Microsoft</name> | ||
| </developer> | ||
| </developers> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.reactivex</groupId> | ||
| <artifactId>rxjava</artifactId> | ||
| <version>1.2.4</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <scope>test</scope> | ||
| <version>4.12</version> | ||
| </dependency> | ||
| </dependencies> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-jar-plugin</artifactId> | ||
| <version>3.1.0</version> | ||
| <configuration> | ||
| <archive> | ||
| <manifest> | ||
| <addDefaultImplementationEntries>true</addDefaultImplementationEntries> | ||
| <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> | ||
| </manifest> | ||
| </archive> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <artifactId>maven-assembly-plugin</artifactId> | ||
| <executions> | ||
| <execution> | ||
| <phase>package</phase> | ||
| <goals> | ||
| <goal>single</goal> | ||
| </goals> | ||
| </execution> | ||
| </executions> | ||
| <configuration> | ||
| <descriptorRefs> | ||
| <descriptorRef>jar-with-dependencies</descriptorRef> | ||
| </descriptorRefs> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.codehaus.mojo</groupId> | ||
| <artifactId>build-helper-maven-plugin</artifactId> | ||
| <version>3.0.0</version> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <version>3.1</version> | ||
| <configuration> | ||
| <source>8</source> | ||
| <target>8</target> | ||
| <debug>true</debug> | ||
| <optimize>true</optimize> | ||
| <compilerArguments> | ||
| <AaddGeneratedAnnotation>true</AaddGeneratedAnnotation> | ||
| <Adebug>true</Adebug> | ||
| </compilerArguments> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-javadoc-plugin</artifactId> | ||
| <version>2.8</version> | ||
| <configuration> | ||
| <excludePackageNames>*.implementation.*;*.utils.*;com.microsoft.schemas._2003._10.serialization;*.blob.core.search</excludePackageNames> | ||
| <bottom> | ||
| <![CDATA[<code> | ||
| /** | ||
| <br />* Copyright (c) Microsoft Corporation. All rights reserved. | ||
| <br />* Licensed under the MIT License. See License.txt in the project root for | ||
| <br />* license information. | ||
| <br />*/ | ||
| </code>]]> | ||
| </bottom> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| ## What is this? | ||
|
|
||
| The "msi-auth-token-provider" jar is a library that enables : | ||
| * Azure VMs and container instances and | ||
| * Web Apps (funcitons included) | ||
| Retrieve authentication tokens for syatem/user assigned [managed identities](https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/overview). | ||
|
|
||
| Thios is a light weight library that does not have many dependencies. The ohnly external library ti depends on is [`RxJava`](https://github.com/ReactiveX/RxJava/releases/tag/v1.2.4) | ||
|
|
||
| ## Usage | ||
| ### Dependency | ||
| Take a dependency on the jar in you pom file like follows | ||
| ```xml | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.microsoft.azure.msi_auth_token_provider</groupId> | ||
| <artifactId>azure-authentication-msi-token-provider</artifactId> | ||
| <version>1.0.0-beta</version> | ||
| </dependency> | ||
| </dependencies> | ||
| ``` | ||
|
|
||
| ### Getting the token | ||
|
|
||
| Add the folowing import statement to get in all the classes in the jar | ||
|
|
||
| ```java | ||
| import com.microsoft.azure.msiAuthTokenProvider.*; | ||
| ``` | ||
|
|
||
| #### Getting a token for system assigned identity | ||
| Use the following code to get the auth token for System assigned identity : | ||
|
|
||
| ``` java | ||
| ... | ||
| MSICredentials credsProvider = MSICredentials.getMSICredentials(); | ||
| String token = credsProvider.getToken(null).toBlocking().value(); | ||
| ... | ||
| ``` | ||
|
|
||
| The `getToken` function returns a [Rx Single](http://reactivex.io/documentation/single.html). What i have shown above is how to use in a syncronous fashion. | ||
|
|
||
| #### Getting a token for user assigned identity | ||
| Use the following code to get the auth token for an User assigned identity : | ||
| ```java | ||
| ... | ||
| MSICredentials credsProvider = MSICredentials.getMSICredentials(); | ||
| credsProvider.updateClientId(clientId); | ||
| String token = credsProvider.getToken(null).toBlocking().value(); | ||
| ... | ||
| ``` | ||
|
|
||
| Where `clientId` is retrieved from the User Assigned Identity (This is currently only supported from within the portal). |
143 changes: 143 additions & 0 deletions
143
...src/main/java/com/microsoft/azure/msiAuthTokenProvider/MSIConfigurationForAppService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| /** | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for | ||
| * license information. | ||
| */ | ||
| package com.microsoft.azure.msiAuthTokenProvider; | ||
|
|
||
| /** | ||
| * Defines the configuration to be used for retrieving access token from | ||
| * within an app-service with system assigned MSI enabled. | ||
| */ | ||
| public class MSIConfigurationForAppService { | ||
| private final String managementEndpoint; | ||
| private String resource; | ||
| private String msiEndpoint; | ||
| private String msiSecret; | ||
| private String clientId; | ||
|
|
||
| /** | ||
| * Creates MSIConfigurationForAppService. | ||
| * | ||
| * @param managementEndpoint azure management endpoint | ||
| */ | ||
| public MSIConfigurationForAppService(String managementEndpoint) { | ||
| this.managementEndpoint = managementEndpoint; | ||
| } | ||
|
|
||
| /** | ||
| * Creates MSIConfigurationForAppService. | ||
| */ | ||
| public MSIConfigurationForAppService() { | ||
| this(MSICredentials.DEFAULT_AZURE_MANAGEMENT_ENDPOINT); | ||
| } | ||
|
|
||
| /** | ||
| * @return the azure management Endpoint. | ||
| */ | ||
| public String managementEndpoint() { | ||
| return this.managementEndpoint; | ||
| } | ||
|
|
||
| /** | ||
| * @return the audience identifying who will consume the token. | ||
| */ | ||
| public String resource() { | ||
| if (this.resource == null) { | ||
| this.resource = this.managementEndpoint; | ||
| } | ||
| return this.resource; | ||
| } | ||
|
|
||
| /** | ||
| * @return the endpoint from which token needs to be retrieved. | ||
| */ | ||
| public String msiEndpoint() { | ||
| if (this.msiEndpoint == null) { | ||
| this.msiEndpoint = System.getenv("MSI_ENDPOINT"); | ||
| } | ||
| return this.msiEndpoint; | ||
| } | ||
|
|
||
| /** | ||
| * @return the secret to use to retrieve the token. | ||
| */ | ||
| public String msiSecret() { | ||
| if (this.msiSecret == null) { | ||
| this.msiSecret = System.getenv("MSI_SECRET"); | ||
| } | ||
| return this.msiSecret; | ||
| } | ||
|
|
||
| /** | ||
| * @return the client id | ||
| */ | ||
| public String msiClientId() { | ||
| return this.clientId; | ||
| } | ||
|
|
||
| /** | ||
| * Specifies the token audience. | ||
| * | ||
| * @param resource the audience of the token. | ||
| * | ||
| * @return MSIConfigurationForAppService | ||
| */ | ||
| public MSIConfigurationForAppService withResource(String resource) { | ||
| this.resource = resource; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Specifies the endpoint from which token needs to retrieved. | ||
| * | ||
| * @param msiEndpoint the token endpoint. | ||
| * | ||
| * @return MSIConfigurationForAppService | ||
| */ | ||
| public MSIConfigurationForAppService withMsiEndpoint(String msiEndpoint) { | ||
| this.msiSecret = msiEndpoint; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Specify the client Id (to be used or user assigned identities) | ||
| * @param clientId the client ID fot eh user assigned identity | ||
| * @return MSIConfigurationForAppService | ||
| */ | ||
| public MSIConfigurationForAppService withClientId(String clientId) { | ||
| this.clientId = clientId; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Specifies secret to use to retrieve the token. | ||
| * | ||
| * @param msiSecret the secret. | ||
| * | ||
| * @return MSIConfigurationForAppService | ||
| */ | ||
| public MSIConfigurationForAppService withMsiSecret(String msiSecret) { | ||
| this.msiSecret = msiSecret; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public MSIConfigurationForAppService clone() { | ||
| MSIConfigurationForAppService copy = new MSIConfigurationForAppService(this.managementEndpoint); | ||
| if (this.resource() != null) { | ||
| copy.withResource(this.resource()); | ||
| } | ||
| if (this.msiEndpoint() != null) { | ||
| copy.withMsiEndpoint(this.msiEndpoint()); | ||
| } | ||
| if (this.msiSecret() != null) { | ||
| copy.withMsiSecret(this.msiSecret()); | ||
| } | ||
| if (this.msiClientId() != null) { | ||
|
|
||
| copy.withClientId(this.msiClientId()); | ||
| } | ||
| return copy; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.