Skip to content

Commit

Permalink
validate credentials when creating client
Browse files Browse the repository at this point in the history
  • Loading branch information
sylviamoss committed Jul 27, 2021
1 parent b603f98 commit 7e8756e
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/packer_registry/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
projectSvc "github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/preview/2019-12-10/client/project_service"
rmmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/preview/2019-12-10/models"
"github.com/hashicorp/hcp-sdk-go/httpclient"
"github.com/hashicorp/packer/internal/packer_registry/env"
)

// Client is an HCP client capable of making requests on behalf of a service principal
Expand All @@ -27,6 +28,13 @@ type Client struct {
// Client authentication requires the following environment variables be set HCP_CLIENT_ID and HCP_CLIENT_SECRET.
// Upon error a HCPClientError will be returned.
func NewClient() (*Client, error) {
if !env.HasHCPCredentials() {
return nil, &ClientError{
StatusCode: InvalidClientConfig,
Err: fmt.Errorf("the client authentication requires both HCP_CLIENT_ID and HCP_CLIENT_SECRET environment variables to be set"),
}
}

cl, err := httpclient.New(httpclient.Config{})
if err != nil {
return nil, &ClientError{
Expand Down

0 comments on commit 7e8756e

Please sign in to comment.