Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Auto-configure Org/Project ID for SPs #46

Merged
merged 2 commits into from
Mar 26, 2024
Merged

Auto-configure Org/Project ID for SPs #46

merged 2 commits into from
Mar 26, 2024

Conversation

dadgar
Copy link
Collaborator

@dadgar dadgar commented Mar 25, 2024

Changes proposed in this PR:

If authenticating the CLI with a service principal and an empty profile, auto-detect the organization and project ID where possible. This enables use cases such as HCP_CLIENT_ID=XXX HCP_CLIENT_SECRET=YYY hcp <command> to work even if no profile exists yet. This can be particularly useful when authenticating in oneshot environments such as a non-interactive run of the Docker container.

How I've tested this PR:

Create a credential file:

$ hcp iam sp keys create test --output-cred-file cred_file.json

Test one-shot authentication with docker:

$ make dev/docker
$ docker run -v .:/root/creds/ -e HCP_CRED_FILE=/root/creds/cred_file.json docker.io/hashicorp/hcp:latest-f76671d hcp projects read
Name:        XXXX
ID:          11eb5071-....
Description: XXX
Created At:  2021-01-06T22:50:07.391Z

$ docker run -e HCP_CLIENT_ID=XXX -e HCP_CLIENT_SECRET=XXX docker.io/hashicorp/hcp:latest-f76671d hcp projects read
Name:        XXXX
ID:          11eb5071-....
Description: XXX
Created At:  2021-01-06T22:50:07.391Z

Prior to this change, this would result in:

$ docker run -e HCP_CLIENT_ID=XXX -e HCP_CLIENT_SECRET=XXX docker.io/hashicorp/hcp:latest-f76671d hcp projects read
Organization ID must be configured before running the command.

Please run hcp profile init to interactively set the Organization ID, or run:

$ hcp profile set organization_id <organization_id>

How I expect reviewers to test this PR:

Code review would be sufficient, but if you want to run the commands, check out the branch and follow the commands I showed in the description and similarly during hcp auth login

Checklist:

  • Tests added if applicable
  • CHANGELOG entry added or label 'pr/no-changelog' added to PR

    Run make changelog-entry for guidance in authoring a changelog entry, and
    commit the resulting file, which should have a name matching your PR number.
    Entries should use imperative present tense (e.g. Add support for...)

dadgar added 2 commits March 25, 2024 16:48
If authenticating the CLI with a service principal and an empty profile,
auto-detect the organization and project ID where possible. This enables
use cases such as `HCP_CLIENT_ID=XXX HCP_CLIENT_SECRET=YYY hcp
<command>` to work even if no profile exists yet. This can be
particularly useful when authenticating in oneshot environments such as
a non-interactive run of the Docker container.
@dadgar dadgar requested a review from tobias-hashicorp March 25, 2024 23:58
@dadgar dadgar marked this pull request as ready for review March 25, 2024 23:58
Copy link
Contributor

@tobias-hashicorp tobias-hashicorp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Just one suggestions to reduce repetition.


I am using Feedback Ladder annotations to make the importance/urgency of comments easier to understand.

Emoji Name Importance/Urgency
🏔 Mountain This change should not go live. Back to the drawing board.
🧗🏻‍♂️ Boulder Has to be addressed before this can be merged.
⚪️ Pebble Needs to be addressed. Can be in a future PR.
Sand Needs to be considered. Can be in a future PR.
✏️ Pen Typo or grammatical error. Can be addressed in a future PR.
💨 Dust "Take it or Leave it"

Comment on lines +163 to +189
if p.OrganizationID != "" || p.ProjectID != "" || err != nil || !tkn.Expiry.After(time.Now()) {
return p, nil
}

// Get the caller identity. If it is a service principal, we can set the
// organization and potentially project automatically. This is particularly
// useful when authenticating the CLI with a service principal and running
// one off commands, where the profile have not been set interactively.
callerIdentityParams := iam_service.NewIamServiceGetCallerIdentityParamsWithContext(ctx)
ident, err := iam.IamServiceGetCallerIdentity(callerIdentityParams, nil)
if err != nil {
return p, nil
}

// Skip if the caller isn't a service principal
if ident.Payload == nil || ident.Payload.Principal == nil || ident.Payload.Principal.Service == nil {
return p, nil
}

// Set the organization and project. Project may be empty.
p.OrganizationID = ident.Payload.Principal.Service.OrganizationID
p.ProjectID = ident.Payload.Principal.Service.ProjectID

// Save the profile.
if err := p.Write(); err != nil {
return nil, fmt.Errorf("failed to save default profile: %w", err)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⌛ This seems to be fairly similar to the above. Would it be an option to extract this into a function SetHierarchyBasedOnSP(...) (ok bool, err error) or such?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah they are close but it isn't that much code and they are different enough I thought duplication was better.

The main difference is login knows it has a valid token, and has to reconfigure the HTTP client, and the cmd.Main doesn't know if there is a token source yet and on error will just ignore vs bubbling up for the login case. The thought there is it lets you run logout / other commands to fix the problem.

@dadgar dadgar merged commit 0de1805 into main Mar 26, 2024
5 checks passed
@dadgar dadgar deleted the f-sp-auto-configure branch March 26, 2024 22:59
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants