Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
MSI Support (#143)
Browse files Browse the repository at this point in the history
* MSI Support - Added NamespaceFromMSI to support creating a namespace via Managed Service Identity

* Updated MSI Method name and doc string

* NamespaceWithEnvironmentBinding - Added GoDoc for Azure Environment variable
  • Loading branch information
patnaikshekhar authored and gavinfish committed Sep 25, 2019
1 parent 472f292 commit bc449e6
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions namespace.go
Expand Up @@ -29,6 +29,7 @@ import (
"runtime"
"strings"

"github.com/Azure/azure-amqp-common-go/v2/aad"
"github.com/Azure/azure-amqp-common-go/v2/auth"
"github.com/Azure/azure-amqp-common-go/v2/cbs"
"github.com/Azure/azure-amqp-common-go/v2/conn"
Expand Down Expand Up @@ -70,6 +71,10 @@ type (
NamespaceOption func(h *Namespace) error
)

const (
serviceBusResourceURI = "https://servicebus.azure.net/"
)

// NamespaceWithConnectionString configures a namespace with the information provided in a Service Bus connection string
func NamespaceWithConnectionString(connStr string) NamespaceOption {
return func(ns *Namespace) error {
Expand Down Expand Up @@ -120,6 +125,34 @@ func NamespaceWithWebSocket() NamespaceOption {
}
}

// NamespaceWithEnvironmentBinding configures a namespace using the environment details. It uses one of the following methods:
//
// 1. Client Credentials: attempt to authenticate with a Service Principal via "AZURE_TENANT_ID", "AZURE_CLIENT_ID" and
// "AZURE_CLIENT_SECRET"
//
// 2. Client Certificate: attempt to authenticate with a Service Principal via "AZURE_TENANT_ID", "AZURE_CLIENT_ID",
// "AZURE_CERTIFICATE_PATH" and "AZURE_CERTIFICATE_PASSWORD"
//
// 3. Managed Identity (MI): attempt to authenticate via the MI assigned to the Azure resource
//
//
// The Azure Environment used can be specified using the name of the Azure Environment set in "AZURE_ENVIRONMENT" var.
func NamespaceWithEnvironmentBinding(name string) NamespaceOption {
return func(ns *Namespace) error {
provider, err := aad.NewJWTProvider(
aad.JWTProviderWithEnvironmentVars(),
aad.JWTProviderWithResourceURI(serviceBusResourceURI),
)
if err != nil {
return err
}

ns.TokenProvider = provider
ns.Name = name
return nil
}
}

// NewNamespace creates a new namespace configured through NamespaceOption(s)
func NewNamespace(opts ...NamespaceOption) (*Namespace, error) {
ns := &Namespace{
Expand Down

0 comments on commit bc449e6

Please sign in to comment.