Files
Packer-Cn/internal/hcp/api/deprecated_client.go
Jenna Goldstrich 8a376a8845 HCP Certificate Authentication Support (#13435)
* Add support for using a certificate file instead of HCP Client Credentials to authenticate to HashiCorp Cloud Platform

* Docs

* Change certificate auth warnings to print to stdio

* rough draft docs

* Update comments

* Parse home directory correctly, default directory certs don't work before this

* cred-file => cred_file, and add unit test

* Fix invalid log, and use full path in log messages and error messages

* Fix original file not being reset in unit test

* Move test statements around to validate Windows behavior

* Use a 'windows friendly' path

* The issue here was the path error'd on mac and linux, but not Windows, when I swapped to the different path then nothing error'd causing everything to fail, I think this should fix it on 3 platforms

* 1.14.1 => 1.14.2
2025-08-12 10:40:55 +05:30

55 lines
1.7 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
// Package api provides access to the HCP Packer Registry API.
package api
import (
"fmt"
packerSvc "github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2021-04-30/client/packer_service"
organizationSvc "github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/stable/2019-12-10/client/organization_service"
projectSvc "github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/stable/2019-12-10/client/project_service"
"github.com/hashicorp/hcp-sdk-go/httpclient"
"github.com/hashicorp/packer/version"
)
// DeprecatedClient is an HCP client capable of making requests on behalf of a service principal
type DeprecatedClient struct {
Packer packerSvc.ClientService
Organization organizationSvc.ClientService
Project projectSvc.ClientService
OrganizationID string
ProjectID string
}
// NewDeprecatedClient returns an authenticated client to a HCP Packer Registry.
// Upon error a HCPClientError will be returned.
func NewDeprecatedClient() (*DeprecatedClient, error) {
// Use NewClient to validate HCP configuration provided by user.
tempClient, err := NewClient()
if err != nil {
return nil, err
}
hcpClientCfg := httpclient.Config{
SourceChannel: fmt.Sprintf("packer/%s", version.PackerVersion.FormattedVersion()),
}
cl, err := httpclient.New(hcpClientCfg)
if err != nil {
return nil, &ClientError{
StatusCode: InvalidClientConfig,
Err: err,
}
}
client := DeprecatedClient{
Packer: packerSvc.New(cl, nil),
Organization: organizationSvc.New(cl, nil),
Project: projectSvc.New(cl, nil),
OrganizationID: tempClient.OrganizationID,
ProjectID: tempClient.ProjectID,
}
return &client, nil
}