mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
feat(cloud): support plaintext file credential store on systems without an OS keychain
› Tenant ID: fails on headless Linux (containers, VMs, CI) because no D-Bus Secret Service is available and the keychain resolver refuses to construct. Add an explicit --insecure-file-store flag that enables dry's plaintext file fallback (~/.config/safedep/creds.json, mode 0600) for writing. Reading is fallback-enabled unconditionally in the credential resolver chain, logout and setup info, so stored file credentials resolve with no extra flags and logout can always clear them. On systems with a working keychain the file provider is never constructed.
This commit is contained in:
@@ -15,7 +15,9 @@ type credentialsResolver func() (*cloud.Credentials, func() error, error)
|
|||||||
// the unauthenticated community analyzer if the API rejects the credentials.
|
// the unauthenticated community analyzer if the API rejects the credentials.
|
||||||
// When no credentials are available, it returns the community analyzer.
|
// When no credentials are available, it returns the community analyzer.
|
||||||
func NewMalysisAnalyzer(config MalysisQueryAnalyzerConfig) (PackageVersionAnalyzer, error) {
|
func NewMalysisAnalyzer(config MalysisQueryAnalyzerConfig) (PackageVersionAnalyzer, error) {
|
||||||
return newMalysisAnalyzer(config, cloudauth.ResolveCredentials)
|
return newMalysisAnalyzer(config, func() (*cloud.Credentials, func() error, error) {
|
||||||
|
return cloudauth.ResolveCredentials()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMalysisAnalyzer(config MalysisQueryAnalyzerConfig,
|
func newMalysisAnalyzer(config MalysisQueryAnalyzerConfig,
|
||||||
|
|||||||
+18
-4
@@ -9,7 +9,10 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var loginFromEnv bool
|
var (
|
||||||
|
loginFromEnv bool
|
||||||
|
loginInsecureFileStore bool
|
||||||
|
)
|
||||||
|
|
||||||
func newLoginCommand() *cobra.Command {
|
func newLoginCommand() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
@@ -20,6 +23,8 @@ func newLoginCommand() *cobra.Command {
|
|||||||
|
|
||||||
cmd.Flags().BoolVar(&loginFromEnv, "from-env", false,
|
cmd.Flags().BoolVar(&loginFromEnv, "from-env", false,
|
||||||
"Read credentials from SAFEDEP_API_KEY and SAFEDEP_TENANT_ID environment variables")
|
"Read credentials from SAFEDEP_API_KEY and SAFEDEP_TENANT_ID environment variables")
|
||||||
|
cmd.Flags().BoolVar(&loginInsecureFileStore, "insecure-file-store", false,
|
||||||
|
"Store credentials in a plaintext file when no OS keychain is available (headless Linux, containers)")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
@@ -89,13 +94,18 @@ func runLogin(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
store, err := cloud.NewKeychainCredentialStore()
|
var opts []cloud.KeychainOption
|
||||||
|
if loginInsecureFileStore {
|
||||||
|
opts = append(opts, cloud.WithInsecureFileFallback())
|
||||||
|
}
|
||||||
|
|
||||||
|
store, err := cloud.NewKeychainCredentialStore(opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||||
Wrap(err).
|
Wrap(err).
|
||||||
WithCode(errcodes.Lifecycle).
|
WithCode(errcodes.Lifecycle).
|
||||||
WithHumanError("Failed to initialize credential store").
|
WithHumanError("Failed to initialize credential store").
|
||||||
WithHelp("Your system may not support secure credential storage"))
|
WithHelp("No OS keychain is available. Re-run with --insecure-file-store to use plaintext file storage, or set SAFEDEP_API_KEY and SAFEDEP_TENANT_ID environment variables"))
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := store.Close(); err != nil {
|
if err := store.Close(); err != nil {
|
||||||
@@ -111,6 +121,10 @@ func runLogin(cmd *cobra.Command, args []string) error {
|
|||||||
WithHelp("Your system may not support secure credential storage"))
|
WithHelp("Your system may not support secure credential storage"))
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.Successf("Credentials saved securely")
|
if loginInsecureFileStore {
|
||||||
|
ui.Successf("Credentials saved")
|
||||||
|
} else {
|
||||||
|
ui.Successf("Credentials saved securely")
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -18,7 +18,7 @@ func newLogoutCommand() *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runLogout(cmd *cobra.Command, args []string) error {
|
func runLogout(cmd *cobra.Command, args []string) error {
|
||||||
store, err := cloud.NewKeychainCredentialStore()
|
store, err := cloud.NewKeychainCredentialStore(cloud.WithInsecureFileFallback())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||||
Wrap(err).
|
Wrap(err).
|
||||||
|
|||||||
+1
-1
@@ -227,7 +227,7 @@ func describeCloudCredentials() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func tryResolveKeychainCredentials() (string, bool) {
|
func tryResolveKeychainCredentials() (string, bool) {
|
||||||
resolver, err := cloud.NewKeychainCredentialResolver(cloud.CredentialTypeAPIKey)
|
resolver, err := cloud.NewKeychainCredentialResolver(cloud.CredentialTypeAPIKey, cloud.WithInsecureFileFallback())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("keychain credential resolver unavailable: %v", err)
|
log.Debugf("keychain credential resolver unavailable: %v", err)
|
||||||
return "", false
|
return "", false
|
||||||
|
|||||||
@@ -16,13 +16,20 @@ import (
|
|||||||
// credentials and a close function that releases keychain resources. The
|
// credentials and a close function that releases keychain resources. The
|
||||||
// close function is always non-nil and safe to call regardless of the error.
|
// close function is always non-nil and safe to call regardless of the error.
|
||||||
//
|
//
|
||||||
|
// The insecure file fallback is always enabled for reading so credentials
|
||||||
|
// stored via `pmg cloud login --insecure-file-store` on systems without an
|
||||||
|
// OS keychain (headless Linux, containers) resolve without extra flags. On
|
||||||
|
// systems with a working keychain the file provider is never constructed.
|
||||||
|
//
|
||||||
// An error is returned when no credentials are available, which callers can
|
// An error is returned when no credentials are available, which callers can
|
||||||
// use to decide whether authenticated cloud features should be enabled.
|
// use to decide whether authenticated cloud features should be enabled.
|
||||||
func ResolveCredentials() (*cloud.Credentials, func() error, error) {
|
func ResolveCredentials(opts ...cloud.KeychainOption) (*cloud.Credentials, func() error, error) {
|
||||||
var resolvers []cloud.CredentialResolver
|
var resolvers []cloud.CredentialResolver
|
||||||
var keychainResolver cloud.CloseableCredentialResolver
|
var keychainResolver cloud.CloseableCredentialResolver
|
||||||
|
|
||||||
keychainResolver, err := cloud.NewKeychainCredentialResolver(cloud.CredentialTypeAPIKey)
|
keychainOpts := append([]cloud.KeychainOption{cloud.WithInsecureFileFallback()}, opts...)
|
||||||
|
|
||||||
|
keychainResolver, err := cloud.NewKeychainCredentialResolver(cloud.CredentialTypeAPIKey, keychainOpts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("Keychain credential resolver not available, skipping: %v", err)
|
log.Debugf("Keychain credential resolver not available, skipping: %v", err)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package cloudauth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/safedep/dry/cloud"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func testKeychainOptions(t *testing.T) []cloud.KeychainOption {
|
||||||
|
t.Helper()
|
||||||
|
return []cloud.KeychainOption{
|
||||||
|
cloud.WithAppName("safedep-test-" + t.Name()),
|
||||||
|
cloud.WithInsecureFileFallbackPath(t.TempDir() + "/creds.json"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveCredentialsFromKeychainStore(t *testing.T) {
|
||||||
|
t.Setenv("SAFEDEP_API_KEY", "")
|
||||||
|
t.Setenv("SAFEDEP_TENANT_ID", "")
|
||||||
|
|
||||||
|
opts := testKeychainOptions(t)
|
||||||
|
|
||||||
|
store, err := cloud.NewKeychainCredentialStore(opts...)
|
||||||
|
require.NoError(t, err)
|
||||||
|
t.Cleanup(func() {
|
||||||
|
require.NoError(t, store.Clear())
|
||||||
|
require.NoError(t, store.Close())
|
||||||
|
})
|
||||||
|
|
||||||
|
require.NoError(t, store.SaveAPIKeyCredential("sk-test-key", "tenant-123"))
|
||||||
|
|
||||||
|
creds, closeFn, err := ResolveCredentials(opts...)
|
||||||
|
require.NoError(t, err)
|
||||||
|
t.Cleanup(func() { require.NoError(t, closeFn()) })
|
||||||
|
|
||||||
|
apiKey, err := creds.GetAPIKey()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "sk-test-key", apiKey)
|
||||||
|
|
||||||
|
tenant, err := creds.GetTenantDomain()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "tenant-123", tenant)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveCredentialsFallsBackToEnv(t *testing.T) {
|
||||||
|
t.Setenv("SAFEDEP_API_KEY", "env-key")
|
||||||
|
t.Setenv("SAFEDEP_TENANT_ID", "env-tenant")
|
||||||
|
|
||||||
|
creds, closeFn, err := ResolveCredentials(testKeychainOptions(t)...)
|
||||||
|
require.NoError(t, err)
|
||||||
|
t.Cleanup(func() { require.NoError(t, closeFn()) })
|
||||||
|
|
||||||
|
apiKey, err := creds.GetAPIKey()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "env-key", apiKey)
|
||||||
|
|
||||||
|
tenant, err := creds.GetTenantDomain()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "env-tenant", tenant)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveCredentialsNoCredentials(t *testing.T) {
|
||||||
|
t.Setenv("SAFEDEP_API_KEY", "")
|
||||||
|
t.Setenv("SAFEDEP_TENANT_ID", "")
|
||||||
|
|
||||||
|
_, closeFn, err := ResolveCredentials(testKeychainOptions(t)...)
|
||||||
|
require.Error(t, err)
|
||||||
|
require.NoError(t, closeFn())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user