Skip to content
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

feat: add azure authentication #67

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions auth/azure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package auth

Check failure on line 1 in auth/azure.go

View workflow job for this annotation

GitHub Actions / Trunk Check

gofmt

Incorrect formatting, autoformat by running 'trunk fmt'

import (
"errors"
"fmt"
"net/http"

"github.com/Azure/go-autorest/autorest"

Check failure on line 8 in auth/azure.go

View workflow job for this annotation

GitHub Actions / Trunk Check

golangci-lint(depguard)

[new] import 'github.com/Azure/go-autorest/autorest' is not allowed from list 'Main'
azure "github.com/Azure/go-autorest/autorest/azure/auth"

Check failure on line 9 in auth/azure.go

View workflow job for this annotation

GitHub Actions / Trunk Check

golangci-lint(depguard)

[new] import 'github.com/Azure/go-autorest/autorest/azure/auth' is not allowed from list 'Main'
)

type AuthType string

type requestBody struct {
GrantType string `json:"grant_type"`

Check failure on line 15 in auth/azure.go

View workflow job for this annotation

GitHub Actions / Trunk Check

golangci-lint(tagliatelle)

[new] json(kebab): got 'grant_type' want 'grant-type'
Jwt string `json:"jwt,omitempty"`
}

// Types of supported authentication.
const (
FederatedAzure = AuthType("azure")
)

// authTypeToGrantType maps authentication type to grant type which will be sent to DSV.
var authTypeToGrantType = map[AuthType]string{

Check failure on line 25 in auth/azure.go

View workflow job for this annotation

GitHub Actions / Trunk Check

golangci-lint(gochecknoglobals)

[new] authTypeToGrantType is a global variable
FederatedAzure: "azure",
}

func (a *authorization) BuildAzureParams() (*requestBody, error) {
resource := "https://management.azure.com/"
authorizer, err := azure.NewAuthorizerFromEnvironmentWithResource(resource)
if err != nil {
return nil, fmt.Errorf("create authorizer: %w", err)
}

p := authorizer.WithAuthorization()

r := &http.Request{}
r, err = autorest.CreatePreparer(p).Prepare(r)
if err != nil {
return nil, fmt.Errorf("generate Azure auth token: %w", err)
}

qualifiedBearer := r.Header.Get("Authorization")
lenPrefix := len("Bearer ")
if len(qualifiedBearer) < lenPrefix {
return nil, errors.New("received invalid bearer token")
}
bearer := qualifiedBearer[lenPrefix:]

data := &requestBody{
GrantType: authTypeToGrantType[FederatedAzure],
Jwt: bearer,
}

return data, nil
}
93 changes: 93 additions & 0 deletions example/azure/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Authentication: Azure

Check notice on line 1 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD022)

[new] Headings should be surrounded by blank lines

Check failure on line 1 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

prettier

Incorrect formatting, autoformat by running 'trunk fmt'
You can use the DSV web UI or the DSV cli to configure authentication using Azure.
For this doc we will use the DSV cli.

Run <br />

Check notice on line 5 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD033)

[new] Inline HTML
`dsv config auth-provider search -e yaml`

Check notice on line 6 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD009)

[new] Trailing spaces
<br />

Check notice on line 7 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD033)

[new] Inline HTML
to see all of your current authentication providers.

Initially, the only authentication provider is Thycotic One, similar to this:
```

Check notice on line 11 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD031)

[new] Fenced code blocks should be surrounded by blank lines

Check notice on line 11 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD040)

[new] Fenced code blocks should have a language specified
created: "2019-11-11T20:29:20Z"
createdBy: users:thy-one:admin@company.com
id: xxxxxxxxxxxxxxxxxxxx
lastModified: "2020-05-18T03:58:15Z"
lastModifiedBy: users:thy-one:admin@company.com
name: thy-one
properties:
baseUri: https://login.thycotic.com/
clientId: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
clientSecret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
type: thycoticone
version: "0"
```

## Azure Authentication Provider

To add an Azure account to act as an authentication provider:<br />

Check notice on line 28 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD033)

[new] Inline HTML
`dsv config auth-provider create --name <name> --type azure --azure-tenant-id <Azure tenant ID>`
where:

* name is the friendly name used in DSV to reference this provider

Check notice on line 32 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD004)

[new] Unordered list style
* type is the authentication provider type; in this case, azure

Check notice on line 33 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD004)

[new] Unordered list style
* the property flag for Azure is `--azure-tenant-id`

Check notice on line 34 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD004)

[new] Unordered list style

To view the resulting addition to the config file, you would use:<br />

Check notice on line 36 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD033)

[new] Inline HTML
`dsv config auth-provider <name> read -e yaml` <br />

Check notice on line 37 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD033)

[new] Inline HTML
where the example name we will use here is azure-prod

The readout would look similar to this:
```

Check notice on line 41 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD031)

[new] Fenced code blocks should be surrounded by blank lines

Check notice on line 41 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD040)

[new] Fenced code blocks should have a language specified
created: "2019-11-12T18:34:49Z"
createdBy: users:thy-one:admin@company.com
-id: xxxxxxxxxxxxxxxxxxxxx
lastModified: "2020-05-18T03:58:15Z"
lastModifiedBy: users:thy-one:admin@company.com
name: azure-prod
properties:
tenantId: xxxxxxxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
type: azure
version: "0"
```

Check notice on line 52 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD031)

[new] Fenced code blocks should be surrounded by blank lines
## Azure User Assigned MSI Example

Check notice on line 53 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD022)

[new] Headings should be surrounded by blank lines

First you will need to configure the User that corresponds to an [Azure User Assigned MSI](https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/overview).
The username is a friendly name within DSV. It does not have to match the MSI username, but the provider must match the resource id of the MSI in Azure.<br />

Check notice on line 56 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD033)

[new] Inline HTML
`dsv user create --username test-api --provider azure-prod --external-id /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/build/providers/Microsoft.ManagedIdentity/userAssignedIdentities/test-api`<br />

Check notice on line 57 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD013)

[new] Line length

Check notice on line 57 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD033)

[new] Inline HTML

## DSV Azure code example

```

Check notice on line 61 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD040)

[new] Fenced code blocks should have a language specified
package main

import (
"fmt"

Check notice on line 65 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
"log"

Check notice on line 66 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
"os"

Check notice on line 67 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs

"github.com/DelineaXPM/dsv-sdk-go/v2/auth"

Check notice on line 69 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
"github.com/DelineaXPM/dsv-sdk-go/v2/vault"

Check notice on line 70 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
)

// Azure authentication example
func main() {
dsv, err := vault.New(vault.Configuration{

Check notice on line 75 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
Credentials: vault.ClientCredential{

Check notice on line 76 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
ClientID: os.Getenv("AZURE_CLIENT_ID"), // CLIENT_ID of the MSI identity you wish to use

Check notice on line 77 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
},

Check notice on line 78 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
Tenant: os.Getenv("DSV_TENANT"), // your tenant name

Check notice on line 79 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
TLD: os.Getenv("DSV_TLD"), // defaults to com change if your domain is au, eu etc

Check notice on line 80 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
Provider: auth.AZURE, // required to enable Azure authentication

Check notice on line 81 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
})

Check notice on line 82 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
if err != nil {

Check notice on line 83 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
log.Fatalf("failed to configure vault: %v", err)

Check notice on line 84 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
}

Check notice on line 85 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs

secret, err := dsv.Secret("<secret path or ID")

Check notice on line 87 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
if err != nil {

Check notice on line 88 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
log.Fatalf("failed to fetch secret: %v", err)

Check notice on line 89 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
}

Check notice on line 90 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
fmt.Printf("\nsecret data: %v\n\n", secret.Data)

Check notice on line 91 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD010)

[new] Hard tabs
}
```

Check notice on line 93 in example/azure/README.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD047)

[new] Files should end with a single newline character
31 changes: 31 additions & 0 deletions example/azure/client_azure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

Check failure on line 1 in example/azure/client_azure.go

View workflow job for this annotation

GitHub Actions / Trunk Check

gofmt

Incorrect formatting, autoformat by running 'trunk fmt'

import (
"fmt"
"log"
"os"

"github.com/DelineaXPM/dsv-sdk-go/v2/auth"

Check failure on line 8 in example/azure/client_azure.go

View workflow job for this annotation

GitHub Actions / Trunk Check

golangci-lint(depguard)

[new] import 'github.com/DelineaXPM/dsv-sdk-go/v2/auth' is not allowed from list 'Main'
"github.com/DelineaXPM/dsv-sdk-go/v2/vault"

Check failure on line 9 in example/azure/client_azure.go

View workflow job for this annotation

GitHub Actions / Trunk Check

golangci-lint(depguard)

[new] import 'github.com/DelineaXPM/dsv-sdk-go/v2/vault' is not allowed from list 'Main'
)

// Azure authentication example
func main() {
dsv, err := vault.New(vault.Configuration{
Credentials: vault.ClientCredential{
ClientID: os.Getenv("AZURE_CLIENT_ID"), // CLIENT_ID of the MSI identity you wish to use
},
Tenant: os.Getenv("DSV_TENANT"), // your tenant name
TLD: os.Getenv("DSV_TLD"), // defaults to com change if your domain is au, eu etc
Provider: auth.AZURE, // required to enable Azure authentication
})
if err != nil {
log.Fatalf("failed to configure vault: %v", err)
}

secret, err := dsv.Secret("<secret path or ID")
if err != nil {
log.Fatalf("failed to fetch secret: %v", err)
}
fmt.Printf("\nsecret data: %v\n\n", secret.Data)
}
2 changes: 1 addition & 1 deletion example/client/client_auth.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main

Check failure on line 1 in example/client/client_auth.go

View workflow job for this annotation

GitHub Actions / Trunk Check

gofmt

Incorrect formatting, autoformat by running 'trunk fmt'

import (
"fmt"
Expand Down Expand Up @@ -29,4 +29,4 @@
}

fmt.Printf("secret data: %v", secret.Data)
}
}
39 changes: 30 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,52 @@ module github.com/DelineaXPM/dsv-sdk-go/v2
go 1.21

require (
github.com/aws/aws-sdk-go v1.50.3
github.com/Azure/go-autorest/autorest v0.11.29
github.com/Azure/go-autorest/autorest/azure/auth v0.5.13
github.com/aws/aws-sdk-go v1.53.8
github.com/magefile/mage v1.15.0
github.com/pterm/pterm v0.12.76
github.com/pterm/pterm v0.12.79
github.com/sheldonhull/magetools v1.0.1
)

require (
atomicgo.dev/cursor v0.2.0 // indirect
atomicgo.dev/keyboard v0.2.9 // indirect
atomicgo.dev/schedule v0.1.0 // indirect
github.com/bitfield/script v0.22.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.22 // indirect
github.com/Azure/go-autorest/autorest/azure/cli v0.4.6 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/bitfield/script v0.22.1 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/frankban/quicktest v1.14.6 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/itchyny/gojq v0.12.12 // indirect
github.com/itchyny/gojq v0.12.13 // indirect
github.com/itchyny/timefmt-go v0.1.5 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
github.com/ztrue/tracerr v0.4.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
mvdan.cc/sh/v3 v3.6.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/tools v0.17.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
mvdan.cc/sh/v3 v3.7.0 // indirect
)
Loading
Loading