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

Enable user profile hce 266 #137

Merged
merged 15 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
3 changes: 3 additions & 0 deletions .changelog/137.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
Allow users to save profile information via environment variables
```
19 changes: 19 additions & 0 deletions config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"os"
"strings"

"github.com/hashicorp/hcp-sdk-go/profile"
)

// The following constants contain the names of environment variables that can
Expand All @@ -29,6 +31,10 @@ const (
envVarSCADAAddress = "HCP_SCADA_ADDRESS"

envVarSCADATLS = "HCP_SCADA_TLS"

envVarHCPOrganizationID = "HCP_ORGANIZATION_ID"

envVarHCPProjectID = "HCP_PROJECT_ID"
)

const (
Expand All @@ -52,6 +58,7 @@ const (
// It will not fail if no or only part of the variables are present.
func FromEnv() HCPConfigOption {
return func(config *hcpConfig) error {

// Read client credentials from the environment, the values will only be
// used if both are provided.
clientID, clientIDOK := os.LookupEnv(envVarClientID)
Expand Down Expand Up @@ -124,6 +131,18 @@ func FromEnv() HCPConfigOption {
config.scadaTLSConfig = scadaTLSConfig
}

// Read user profile information from the environment, the values will only be
// used if both fields are provided.
hcpOrganizationID, hcpOrganizationIDOK := os.LookupEnv(envVarHCPOrganizationID)
hcpProjectID, hcpProjectIDOK := os.LookupEnv(envVarHCPProjectID)

if hcpOrganizationIDOK && hcpProjectIDOK {
userProfile := profile.UserProfile{OrganizationID: hcpOrganizationID, ProjectID: hcpProjectID}
if err := apply(config, WithProfile(&userProfile)); err != nil {
return fmt.Errorf("failed to configure profile fields based on environment variables (%s, %s): %w", envVarHCPOrganizationID, envVarHCPProjectID, err)
}
}

return nil
}
}
Expand Down
12 changes: 12 additions & 0 deletions config/hcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import (
"net/url"

"github.com/hashicorp/hcp-sdk-go/auth"
"github.com/hashicorp/hcp-sdk-go/profile"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)

// HCPConfig provides configuration values that are useful to interact with HCP.
type HCPConfig interface {

//Profile will return the user's configured profile information
Profile() *profile.UserProfile

// TokenSource will return a token that can be used to authenticate to HCP.
oauth2.TokenSource

Expand Down Expand Up @@ -84,6 +89,13 @@ type hcpConfig struct {
// session is responsible for getting an access token fron our identity provider.
// A mock can be used in tests.
session auth.Session

Copy link
Contributor

Choose a reason for hiding this comment

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

a bonus for this PR or a follow-up: let's use a Session pointer

// profile is user's the organization id and project id
profile *profile.UserProfile
}

func (c *hcpConfig) Profile() *profile.UserProfile {
return c.profile
}

func (c *hcpConfig) Token() (*oauth2.Token, error) {
Expand Down
2 changes: 2 additions & 0 deletions config/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/hcp-sdk-go/auth"
"github.com/hashicorp/hcp-sdk-go/profile"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
Expand Down Expand Up @@ -74,6 +75,7 @@ func NewHCPConfig(opts ...HCPConfigOption) (HCPConfig, error) {
Scopes: []string{"openid", "offline_access"},
},
session: &auth.UserSession{},
profile: &profile.UserProfile{},

portalURL: portalURL,

Expand Down
6 changes: 5 additions & 1 deletion config/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/hashicorp/hcp-sdk-go/auth"
"github.com/hashicorp/hcp-sdk-go/profile"
requirepkg "github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -36,13 +37,16 @@ func TestNew_Options(t *testing.T) {
WithPortalURL("https://my-portal:1234"),
WithAPI("my-api:2345", &tls.Config{}),
WithSCADA("my-scada:3456", &tls.Config{}),
WithProfile(&profile.UserProfile{OrganizationID: "org-id-123", ProjectID: "proj-id-123"}),
)

//profile := config.Profile()
require.NoError(err)
// Ensure the values have been set accordingly
require.Equal("https://my-portal:1234", config.PortalURL().String())
require.Equal("my-api:2345", config.APIAddress())
require.Equal("my-scada:3456", config.SCADAAddress())
require.Equal("org-id-123", config.Profile().OrganizationID)
require.Equal("proj-id-123", config.Profile().ProjectID)
}

func TestNew_MockSession(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions config/with.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"

"github.com/hashicorp/hcp-sdk-go/auth"
"github.com/hashicorp/hcp-sdk-go/profile"
)

// WithClientCredentials credentials is an option that can be used to set
Expand Down Expand Up @@ -125,3 +126,11 @@ func WithSession(s auth.Session) HCPConfigOption {
return nil
}
}

// WithProfile is an option that can be used to provide a custom UserProfile struct.
func WithProfile(p *profile.UserProfile) HCPConfigOption {
return func(config *hcpConfig) error {
config.profile = p
return nil
}
}
15 changes: 15 additions & 0 deletions config/with_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/hashicorp/hcp-sdk-go/auth"
"github.com/hashicorp/hcp-sdk-go/profile"
requirepkg "github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -105,3 +106,17 @@ func TestWith_Session(t *testing.T) {
// Ensure Sessions is an empty MockSession object
require.Equal(&auth.MockSession{}, config.session)
}

func TestWith_Profile(t *testing.T) {
require := requirepkg.New(t)

// Exercise
config := &hcpConfig{}
profile := &profile.UserProfile{OrganizationID: "org-id-1234", ProjectID: "project-id-1234"}
require.NoError(apply(config, WithProfile(profile)))

//Ensure UserProfile fields match configured values
require.Equal("org-id-1234", config.Profile().OrganizationID)
require.Equal("project-id-1234", config.Profile().ProjectID)

}
2 changes: 2 additions & 0 deletions httpclient/httpclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

consul "github.com/hashicorp/hcp-sdk-go/clients/cloud-consul-service/stable/2021-02-04/client/consul_service"
"github.com/hashicorp/hcp-sdk-go/config"
"github.com/hashicorp/hcp-sdk-go/profile"
)

func TestNew(t *testing.T) {
Expand Down Expand Up @@ -122,6 +123,7 @@ func TestNew(t *testing.T) {
config.WithClientCredentials(clientID, clientSecret),
config.WithAuth(ts.URL, tlsConfig),
config.WithAPI(serverURL.Host, tlsConfig),
config.WithProfile(&profile.UserProfile{OrganizationID: orgID, ProjectID: projID}),
)
require.NoError(t, err)

Expand Down
11 changes: 11 additions & 0 deletions profile/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package profile

// UserProfile represents the user's configured profile information.
type UserProfile struct {

// OrganizationID is the user's organization ID.
OrganizationID string

// ProjectID is the user's project ID.
ProjectID string
}