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

Add support for Korifi to the cf auth command #2296

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 25 additions & 1 deletion command/v7/auth_command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v7

import (
"errors"
"fmt"

"code.cloudfoundry.org/cli/api/uaa/constant"
Expand Down Expand Up @@ -77,6 +78,29 @@ func (cmd AuthCommand) Execute(args []string) error {
grantType = constant.GrantTypeClientCredentials
credentials["client_id"] = username
credentials["client_secret"] = password
} else if cmd.Config.IsCFOnK8s() {
prompts, err := cmd.Actor.GetLoginPrompts()
if err != nil {
return err
}
prompt, ok := prompts["k8s-auth-info"]
if !ok {
return errors.New("kubernetes login context is missing")
}

userFound := false
for _, val := range prompt.Entries {
if val == username {
userFound = true
break
}
}
if !userFound {
return errors.New("kubernetes user not found in configuration: " + username)
}
credentials = map[string]string{
"k8s-auth-info": username,
}
} else {
credentials = map[string]string{
"username": username,
Expand Down Expand Up @@ -118,7 +142,7 @@ func (cmd AuthCommand) getUsernamePassword() (string, string, error) {
if password == "" {
if envPassword := cmd.Config.CFPassword(); envPassword != "" {
password = envPassword
} else {
} else if !cmd.Config.IsCFOnK8s() {
passwordMissing = true
}
}
Expand Down
65 changes: 59 additions & 6 deletions command/v7/auth_command_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v7_test

import (
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"errors"

"code.cloudfoundry.org/cli/api/uaa"
Expand All @@ -18,12 +19,13 @@ import (

var _ = Describe("auth Command", func() {
var (
cmd AuthCommand
testUI *ui.UI
fakeActor *v7fakes.FakeActor
fakeConfig *commandfakes.FakeConfig
binaryName string
err error
cmd AuthCommand
testUI *ui.UI
fakeActor *v7fakes.FakeActor
fakeConfig *commandfakes.FakeConfig
binaryName string
err error
k8sLoginPrompts map[string]coreconfig.AuthPrompt
)

BeforeEach(func() {
Expand All @@ -43,6 +45,11 @@ var _ = Describe("auth Command", func() {
fakeConfig.BinaryNameReturns(binaryName)
fakeConfig.UAAOAuthClientReturns("cf")
fakeConfig.APIVersionReturns("3.99.0")
k8sLoginPrompts = map[string]coreconfig.AuthPrompt{
"k8s-auth-info": {
Entries: []string{"myuser"},
},
}
})

JustBeforeEach(func() {
Expand Down Expand Up @@ -113,6 +120,17 @@ var _ = Describe("auth Command", func() {
MissingPassword: true,
}))
})

When("authenticating against Korifi", func() {
BeforeEach(func() {
fakeConfig.IsCFOnK8sReturns(true)
fakeActor.GetLoginPromptsReturns(k8sLoginPrompts, nil)
})

It("succeeds", func() {
Expect(err).NotTo(HaveOccurred())
})
})
})
})

Expand Down Expand Up @@ -298,6 +316,41 @@ var _ = Describe("auth Command", func() {
})
})
})

When("authenticating against Korifi", func() {
BeforeEach(func() {
cmd.RequiredArgs.Username = "myuser"
fakeConfig.IsCFOnK8sReturns(true)
fakeActor.GetLoginPromptsReturns(k8sLoginPrompts, nil)
})

When("the specified username doesn't match any k8s context", func() {
BeforeEach(func() {
cmd.RequiredArgs.Username = "some-unknown-user"
})

It("errors", func() {
Expect(err).To(MatchError(errors.New("kubernetes user not found in configuration: some-unknown-user")))
})
})

When("the prompts don't contain k8s authentication information", func() {
BeforeEach(func() {
k8sLoginPrompts = map[string]coreconfig.AuthPrompt{
"some-other-auth-info": {
Entries: []string{"myuser"},
},
}
fakeActor.GetLoginPromptsReturns(k8sLoginPrompts, nil)
})

It("errors", func() {
Expect(err).To(MatchError(errors.New("kubernetes login context is missing")))
})

})

})
})

When("a user has manually added their client credentials to the config file", func() {
Expand Down