Last active
October 9, 2024 02:28
-
-
Save unclebean/c4d1fa43702b8044581ed1cbf47db00f to your computer and use it in GitHub Desktop.
azure
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
| 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()); | |
| } | |
| } |
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
| 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."); | |
| } | |
| } |
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
| 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()); | |
| }); | |
| } | |
| } |
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
| 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/ |
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
| 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/ |
Author
Author
Author
az role assignment list --assignee [uami-client-id]
Author
az aks update --resource-group [resource-group-name] --name [aks-cluster-name] --assign-identity [uami-resource-id]
Author
Author
az account get-access-token --resource https://management.azure.com/ --identity --name [uami-name] --resource-group [resource-group-name]
Author
az role assignment list --assignee [uami-client-id] --output table
Author
az aks show --resource-group [resource-group] --name [aks-cluster-name] --query identity
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
az aks show --resource-group --name --query identity