Skip to content

Instantly share code, notes, and snippets.

@unclebean
Last active October 9, 2024 02:28
Show Gist options
  • Select an option

  • Save unclebean/c4d1fa43702b8044581ed1cbf47db00f to your computer and use it in GitHub Desktop.

Select an option

Save unclebean/c4d1fa43702b8044581ed1cbf47db00f to your computer and use it in GitHub Desktop.
azure
import com.azure.identity.ManagedIdentityCredential;
import com.azure.identity.ManagedIdentityCredentialBuilder;
import com.azure.core.credential.TokenRequestContext;
import com.azure.core.credential.AccessToken;
public class AccessTokenFetcher {
public static void main(String[] args) {
// Define the resource you are requesting the token for (e.g., Azure Cognitive Search)
String resource = "https://search.azure.com/";
// Create a ManagedIdentityCredential using the client ID of your UAMI
ManagedIdentityCredential managedIdentityCredential = new ManagedIdentityCredentialBuilder()
.clientId("<UAMI_CLIENT_ID>") // Replace with your UAMI Client ID
.build();
// Define the token request context
TokenRequestContext requestContext = new TokenRequestContext()
.addScopes(resource + ".default");
// Fetch the access token
AccessToken token = managedIdentityCredential.getToken(requestContext).block();
// Print the token
System.out.println("Access Token: " + token.getToken());
}
}
import com.azure.identity.ManagedIdentityCredentialBuilder;
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobClientBuilder;
import com.azure.storage.blob.models.BlobDownloadToFileOptions;
public class BlobStorageExample {
public static void main(String[] args) {
// Specify the Client ID of your User-Assigned Managed Identity (UAMI)
String userAssignedClientId = "<your-UAMI-client-id>";
// Create a ManagedIdentityCredential using UAMI
ManagedIdentityCredentialBuilder credentialBuilder = new ManagedIdentityCredentialBuilder()
.clientId(userAssignedClientId);
// Blob Storage URL (e.g., https://<your-storage-account>.blob.core.windows.net/<container-name>/<blob-name>)
String blobUrl = "https://<your-storage-account>.blob.core.windows.net/<container-name>/<blob-name>";
// Create a BlobClient using the credential
BlobClient blobClient = new BlobClientBuilder()
.credential(credentialBuilder.build())
.endpoint(blobUrl)
.buildClient();
// Download the blob to a local file
blobClient.downloadToFile("<path-to-local-file>");
System.out.println("Blob downloaded successfully.");
}
}
import com.azure.core.credential.TokenCredential;
import com.azure.identity.ManagedIdentityCredentialBuilder;
import com.azure.search.documents.SearchClient;
import com.azure.search.documents.SearchClientBuilder;
import com.azure.search.documents.models.SearchResults;
import com.azure.search.documents.models.SearchQuery;
public class CognitiveSearchExample {
public static void main(String[] args) {
// UAMI client ID
String userAssignedClientId = "<your-UAMI-client-id>";
// Build the credential using UAMI
TokenCredential credential = new ManagedIdentityCredentialBuilder()
.clientId(userAssignedClientId)
.build();
// The endpoint URL of your Azure Cognitive Search service
String searchEndpoint = "https://<your-search-service-name>.search.windows.net";
// Your search index name
String indexName = "<your-index-name>";
// Build the SearchClient
SearchClient searchClient = new SearchClientBuilder()
.credential(credential) // Pass the credential for authentication
.endpoint(searchEndpoint) // Endpoint of your search service
.indexName(indexName) // The name of your search index
.buildClient();
// Perform a search query
String searchQuery = "azure"; // Example search term
SearchResults searchResults = searchClient.search(searchQuery);
// Iterate through and print the search results
searchResults.getResults().forEach(result -> {
System.out.println("Document: " + result.getDocument());
});
}
}
azure:
storage:
account-name: your-storage-account-name
endpoint: https://your-storage-account-name.blob.core.windows.net
use-managed-identity: true
cognitive-search:
service-name: your-search-service-name
endpoint: https://your-search-service-name.search.windows.net
use-managed-identity: true
spring:
datasource:
url: jdbc:postgresql://your-postgres-server.postgres.database.azure.com:5432/your-database-name
driver-class-name: org.postgresql.Driver
hikari:
maximum-pool-size: 10
authentication-method: managed-identity
azure:
msal:
client-id: your-umi-client-id
tenant-id: your-tenant-id
authority-host: https://login.microsoftonline.com/
azure:
storage:
account-name: your-storage-account-name
endpoint: https://your-storage-account-name.blob.core.windows.net
use-managed-identity: true
cognitive-search:
service-name: your-search-service-name
endpoint: https://your-search-service-name.search.windows.net
use-managed-identity: true
spring:
datasource:
url: jdbc:postgresql://your-postgres-server.postgres.database.azure.com:5432/your-database-name
driver-class-name: org.postgresql.Driver
hikari:
maximum-pool-size: 10
authentication-method: managed-identity
azure:
msal:
client-id: your-umi-client-id
tenant-id: your-tenant-id
authority-host: https://login.microsoftonline.com/
@unclebean

Copy link
Copy Markdown
Author

az aks show --resource-group --name --query identity

@unclebean

Copy link
Copy Markdown
Author

@unclebean

unclebean commented Oct 4, 2024

Copy link
Copy Markdown
Author

az role assignment list --assignee [uami-client-id]

@unclebean

Copy link
Copy Markdown
Author

az aks update --resource-group [resource-group-name] --name [aks-cluster-name] --assign-identity [uami-resource-id]

@unclebean

Copy link
Copy Markdown
Author

@unclebean

unclebean commented Oct 4, 2024

Copy link
Copy Markdown
Author

az account get-access-token --resource https://management.azure.com/ --identity --name [uami-name] --resource-group [resource-group-name]

@unclebean

unclebean commented Oct 4, 2024

Copy link
Copy Markdown
Author

az role assignment list --assignee [uami-client-id] --output table

@unclebean

Copy link
Copy Markdown
Author

az aks show --resource-group [resource-group] --name [aks-cluster-name] --query identity

@unclebean

Copy link
Copy Markdown
Author

kubectl get pods -n [namespace] -o custom-columns=NAME:.metadata.name,SERVICEACCOUNT:.spec.serviceAccountName

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment