Skip to content

Commit

Permalink
Change to azd auth serve
Browse files Browse the repository at this point in the history
  • Loading branch information
jongio committed Jun 27, 2024
1 parent 8dff969 commit c69564c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
1 change: 0 additions & 1 deletion cli/azd/.vscode/cspell-azd-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ gotest
gotestsum
hotspot
iidfile
imds
ineffassign
jaegertracing
javac
Expand Down
7 changes: 3 additions & 4 deletions cli/azd/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ func authActions(root *actions.ActionDescriptor) *actions.ActionDescriptor {
ActionResolver: newLogoutAction,
})

// Register the new `local-imds` command
group.Add("local-imds", &actions.ActionDescriptorOptions{
Command: newLocalIMDSCmd("auth"),
ActionResolver: newLocalIMDSAction,
group.Add("serve", &actions.ActionDescriptorOptions{
Command: newServeCmd("auth"),
ActionResolver: newServeAction,
})

return group
Expand Down
29 changes: 14 additions & 15 deletions cli/azd/cmd/auth_local_imds.go → cli/azd/cmd/auth_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type TokenResponse struct {
}

// tokenHandler handles token requests.
func (lia *localIMDSAction) tokenHandler(w http.ResponseWriter, r *http.Request) {
func (serve *serveAction) tokenHandler(w http.ResponseWriter, r *http.Request) {
resource := r.URL.Query().Get("resource")
if resource == "" {
resource = "https://management.azure.com/"
Expand All @@ -39,7 +39,7 @@ func (lia *localIMDSAction) tokenHandler(w http.ResponseWriter, r *http.Request)
ctx := context.Background()
var cred azcore.TokenCredential

cred, err := lia.credentialProvider(ctx, &auth.CredentialForCurrentUserOptions{
cred, err := serve.credentialProvider(ctx, &auth.CredentialForCurrentUserOptions{
NoPrompt: true,
TenantID: "",
})
Expand Down Expand Up @@ -67,10 +67,9 @@ func (lia *localIMDSAction) tokenHandler(w http.ResponseWriter, r *http.Request)
}
}

// startIMDSServer starts the IMDS emulator server.
func (lia *localIMDSAction) startIMDSServer(port string) {
http.HandleFunc("/MSI/token", lia.tokenHandler)
http.HandleFunc("/metadata/identity/oauth2/token", lia.tokenHandler)
func (serve *serveAction) start(port string) {
http.HandleFunc("/MSI/token", serve.tokenHandler)
http.HandleFunc("/metadata/identity/oauth2/token", serve.tokenHandler)

srv := &http.Server{
Addr: ":" + port,
Expand Down Expand Up @@ -102,41 +101,41 @@ func (lia *localIMDSAction) startIMDSServer(port string) {
os.Exit(0)
}

func newLocalIMDSCmd(parent string) *cobra.Command {
func newServeCmd(parent string) *cobra.Command {
return &cobra.Command{
Use: "local-imds",
Short: "Starts a local IMDS emulator",
Use: "serve",
Short: "Starts a local Managed Identity endpoint for development purposes.",
Annotations: map[string]string{
loginCmdParentAnnotation: parent,
},
}
}

type localIMDSAction struct {
type serveAction struct {
console input.Console
credentialProvider CredentialProviderFn
formatter output.Formatter
writer io.Writer
}

func newLocalIMDSAction(
func newServeAction(
console input.Console,
credentialProvider CredentialProviderFn,
formatter output.Formatter,
writer io.Writer) actions.Action {
return &localIMDSAction{
return &serveAction{
console: console,
credentialProvider: credentialProvider,
formatter: formatter,
writer: writer,
}
}

func (lia *localIMDSAction) Run(ctx context.Context) (*actions.ActionResult, error) {
port := os.Getenv("IMDS_PORT")
func (serve *serveAction) Run(ctx context.Context) (*actions.ActionResult, error) {
port := os.Getenv("AZD_AUTH_SERVER_PORT")
if port == "" {
port = "53028"
}
lia.startIMDSServer(port)
serve.start(port)
return nil, nil
}
18 changes: 18 additions & 0 deletions cli/azd/cmd/testdata/TestUsage-azd-auth-serve.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

Starts a local Managed Identity endpoint for development purposes.

Usage
azd auth serve [flags]

Flags
--docs : Opens the documentation for azd auth serve in your web browser.
-h, --help : Gets help for serve.

Global Flags
-C, --cwd string : Sets the current working directory.
--debug : Enables debugging and diagnostics logging.
--no-prompt : Accepts the default value instead of prompting, or it fails if there is no default.

Find a bug? Want to let us know how we're doing? Fill out this brief survey: https://aka.ms/azure-dev/hats.


1 change: 1 addition & 0 deletions cli/azd/cmd/testdata/TestUsage-azd-auth.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Usage
Available Commands
login : Log in to Azure.
logout : Log out of Azure.
serve : Starts a local Managed Identity endpoint for development purposes.

Flags
--docs : Opens the documentation for azd auth in your web browser.
Expand Down

0 comments on commit c69564c

Please sign in to comment.