mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-10-30 18:17:55 +00:00 
			
		
		
		
	backport of commit 69fda8da76 (#22923)
				
					
				
			Co-authored-by: Theron Voran <tvoran@users.noreply.github.com>
This commit is contained in:
		 hc-github-team-secure-vault-core
					hc-github-team-secure-vault-core
				
			
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			 GitHub
						GitHub
					
				
			
						parent
						
							8ca6240c55
						
					
				
				
					commit
					d66c8ff259
				
			| @@ -20,6 +20,15 @@ import ( | |||||||
| 	"github.com/hashicorp/vault/sdk/helper/pluginruntimeutil" | 	"github.com/hashicorp/vault/sdk/helper/pluginruntimeutil" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | const ( | ||||||
|  | 	// Labels for plugin container ownership | ||||||
|  | 	labelVaultPID           = "com.hashicorp.vault.pid" | ||||||
|  | 	labelVaultClusterID     = "com.hashicorp.vault.cluster.id" | ||||||
|  | 	labelVaultPluginName    = "com.hashicorp.vault.plugin.name" | ||||||
|  | 	labelVaultPluginVersion = "com.hashicorp.vault.plugin.version" | ||||||
|  | 	labelVaultPluginType    = "com.hashicorp.vault.plugin.type" | ||||||
|  | ) | ||||||
|  |  | ||||||
| type PluginClientConfig struct { | type PluginClientConfig struct { | ||||||
| 	Name            string | 	Name            string | ||||||
| 	PluginType      consts.PluginType | 	PluginType      consts.PluginType | ||||||
| @@ -123,7 +132,10 @@ func (rc runConfig) makeConfig(ctx context.Context) (*plugin.ClientConfig, error | |||||||
| 			Hash:     sha256.New(), | 			Hash:     sha256.New(), | ||||||
| 		} | 		} | ||||||
| 	} else { | 	} else { | ||||||
| 		containerCfg := rc.containerConfig(cmd.Env) | 		containerCfg, err := rc.containerConfig(ctx, cmd.Env) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return nil, err | ||||||
|  | 		} | ||||||
| 		clientConfig.SkipHostEnv = true | 		clientConfig.SkipHostEnv = true | ||||||
| 		clientConfig.RunnerFunc = containerCfg.NewContainerRunner | 		clientConfig.RunnerFunc = containerCfg.NewContainerRunner | ||||||
| 		clientConfig.UnixSocketConfig = &plugin.UnixSocketConfig{ | 		clientConfig.UnixSocketConfig = &plugin.UnixSocketConfig{ | ||||||
| @@ -133,7 +145,11 @@ func (rc runConfig) makeConfig(ctx context.Context) (*plugin.ClientConfig, error | |||||||
| 	return clientConfig, nil | 	return clientConfig, nil | ||||||
| } | } | ||||||
|  |  | ||||||
| func (rc runConfig) containerConfig(env []string) *plugincontainer.Config { | func (rc runConfig) containerConfig(ctx context.Context, env []string) (*plugincontainer.Config, error) { | ||||||
|  | 	clusterID, err := rc.Wrapper.ClusterID(ctx) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
| 	cfg := &plugincontainer.Config{ | 	cfg := &plugincontainer.Config{ | ||||||
| 		Image:  rc.image, | 		Image:  rc.image, | ||||||
| 		Tag:    rc.imageTag, | 		Tag:    rc.imageTag, | ||||||
| @@ -143,9 +159,14 @@ func (rc runConfig) containerConfig(env []string) *plugincontainer.Config { | |||||||
| 		GroupAdd: os.Getgid(), | 		GroupAdd: os.Getgid(), | ||||||
| 		Runtime:  consts.DefaultContainerPluginOCIRuntime, | 		Runtime:  consts.DefaultContainerPluginOCIRuntime, | ||||||
| 		Labels: map[string]string{ | 		Labels: map[string]string{ | ||||||
| 			"managed-by": "hashicorp.com/vault", | 			labelVaultPID:           strconv.Itoa(os.Getpid()), | ||||||
|  | 			labelVaultClusterID:     clusterID, | ||||||
|  | 			labelVaultPluginName:    rc.PluginClientConfig.Name, | ||||||
|  | 			labelVaultPluginType:    rc.PluginClientConfig.PluginType.String(), | ||||||
|  | 			labelVaultPluginVersion: rc.PluginClientConfig.Version, | ||||||
| 		}, | 		}, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Use rc.command and rc.args directly instead of cmd.Path and cmd.Args, as | 	// Use rc.command and rc.args directly instead of cmd.Path and cmd.Args, as | ||||||
| 	// exec.Command may mutate the provided command. | 	// exec.Command may mutate the provided command. | ||||||
| 	if rc.command != "" { | 	if rc.command != "" { | ||||||
| @@ -163,7 +184,7 @@ func (rc runConfig) containerConfig(env []string) *plugincontainer.Config { | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return cfg | 	return cfg, nil | ||||||
| } | } | ||||||
|  |  | ||||||
| func (rc runConfig) run(ctx context.Context) (*plugin.Client, error) { | func (rc runConfig) run(ctx context.Context) (*plugin.Client, error) { | ||||||
| @@ -240,6 +261,11 @@ func (r *PluginRunner) RunConfig(ctx context.Context, opts ...RunOpt) (*plugin.C | |||||||
| 		sha256:        r.Sha256, | 		sha256:        r.Sha256, | ||||||
| 		env:           r.Env, | 		env:           r.Env, | ||||||
| 		runtimeConfig: r.RuntimeConfig, | 		runtimeConfig: r.RuntimeConfig, | ||||||
|  | 		PluginClientConfig: PluginClientConfig{ | ||||||
|  | 			Name:       r.Name, | ||||||
|  | 			PluginType: r.Type, | ||||||
|  | 			Version:    r.Version, | ||||||
|  | 		}, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	for _, opt := range opts { | 	for _, opt := range opts { | ||||||
|   | |||||||
| @@ -432,11 +432,16 @@ func (m *mockRunnerUtil) MlockEnabled() bool { | |||||||
| 	return args.Bool(0) | 	return args.Bool(0) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (m *mockRunnerUtil) ClusterID(ctx context.Context) (string, error) { | ||||||
|  | 	return "1234", nil | ||||||
|  | } | ||||||
|  |  | ||||||
| func TestContainerConfig(t *testing.T) { | func TestContainerConfig(t *testing.T) { | ||||||
| 	dummySHA, err := hex.DecodeString("abc123") | 	dummySHA, err := hex.DecodeString("abc123") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
|  | 	myPID := strconv.Itoa(os.Getpid()) | ||||||
| 	for name, tc := range map[string]struct { | 	for name, tc := range map[string]struct { | ||||||
| 		rc       runConfig | 		rc       runConfig | ||||||
| 		expected plugincontainer.Config | 		expected plugincontainer.Config | ||||||
| @@ -460,8 +465,11 @@ func TestContainerConfig(t *testing.T) { | |||||||
| 						MagicCookieKey:   "magic_cookie_key", | 						MagicCookieKey:   "magic_cookie_key", | ||||||
| 						MagicCookieValue: "magic_cookie_value", | 						MagicCookieValue: "magic_cookie_value", | ||||||
| 					}, | 					}, | ||||||
| 					Logger:   hclog.NewNullLogger(), | 					Logger:     hclog.NewNullLogger(), | ||||||
| 					AutoMTLS: true, | 					AutoMTLS:   true, | ||||||
|  | 					Name:       "some-plugin", | ||||||
|  | 					PluginType: consts.PluginTypeCredential, | ||||||
|  | 					Version:    "v0.1.0", | ||||||
| 				}, | 				}, | ||||||
| 			}, | 			}, | ||||||
| 			expected: plugincontainer.Config{ | 			expected: plugincontainer.Config{ | ||||||
| @@ -477,7 +485,11 @@ func TestContainerConfig(t *testing.T) { | |||||||
| 					fmt.Sprintf("%s=%t", PluginAutoMTLSEnv, true), | 					fmt.Sprintf("%s=%t", PluginAutoMTLSEnv, true), | ||||||
| 				}, | 				}, | ||||||
| 				Labels: map[string]string{ | 				Labels: map[string]string{ | ||||||
| 					"managed-by": "hashicorp.com/vault", | 					labelVaultPID:           myPID, | ||||||
|  | 					labelVaultClusterID:     "1234", | ||||||
|  | 					labelVaultPluginName:    "some-plugin", | ||||||
|  | 					labelVaultPluginType:    "auth", | ||||||
|  | 					labelVaultPluginVersion: "v0.1.0", | ||||||
| 				}, | 				}, | ||||||
| 				Runtime:  consts.DefaultContainerPluginOCIRuntime, | 				Runtime:  consts.DefaultContainerPluginOCIRuntime, | ||||||
| 				GroupAdd: os.Getgid(), | 				GroupAdd: os.Getgid(), | ||||||
| @@ -505,8 +517,11 @@ func TestContainerConfig(t *testing.T) { | |||||||
| 						MagicCookieKey:   "magic_cookie_key", | 						MagicCookieKey:   "magic_cookie_key", | ||||||
| 						MagicCookieValue: "magic_cookie_value", | 						MagicCookieValue: "magic_cookie_value", | ||||||
| 					}, | 					}, | ||||||
| 					Logger:   hclog.NewNullLogger(), | 					Logger:     hclog.NewNullLogger(), | ||||||
| 					AutoMTLS: true, | 					AutoMTLS:   true, | ||||||
|  | 					Name:       "some-plugin", | ||||||
|  | 					PluginType: consts.PluginTypeCredential, | ||||||
|  | 					Version:    "v0.1.0", | ||||||
| 				}, | 				}, | ||||||
| 			}, | 			}, | ||||||
| 			expected: plugincontainer.Config{ | 			expected: plugincontainer.Config{ | ||||||
| @@ -519,7 +534,11 @@ func TestContainerConfig(t *testing.T) { | |||||||
| 					fmt.Sprintf("%s=%t", PluginAutoMTLSEnv, true), | 					fmt.Sprintf("%s=%t", PluginAutoMTLSEnv, true), | ||||||
| 				}, | 				}, | ||||||
| 				Labels: map[string]string{ | 				Labels: map[string]string{ | ||||||
| 					"managed-by": "hashicorp.com/vault", | 					labelVaultPID:           myPID, | ||||||
|  | 					labelVaultClusterID:     "1234", | ||||||
|  | 					labelVaultPluginName:    "some-plugin", | ||||||
|  | 					labelVaultPluginType:    "auth", | ||||||
|  | 					labelVaultPluginVersion: "v0.1.0", | ||||||
| 				}, | 				}, | ||||||
| 				Runtime:      "some-oci-runtime", | 				Runtime:      "some-oci-runtime", | ||||||
| 				GroupAdd:     os.Getgid(), | 				GroupAdd:     os.Getgid(), | ||||||
| @@ -540,7 +559,8 @@ func TestContainerConfig(t *testing.T) { | |||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				t.Fatal(err) | 				t.Fatal(err) | ||||||
| 			} | 			} | ||||||
| 			cfg := tc.rc.containerConfig(cmd.Env) | 			cfg, err := tc.rc.containerConfig(context.Background(), cmd.Env) | ||||||
|  | 			require.NoError(t, err) | ||||||
| 			require.Equal(t, tc.expected, *cfg) | 			require.Equal(t, tc.expected, *cfg) | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -33,6 +33,7 @@ type RunnerUtil interface { | |||||||
| 	ResponseWrapData(ctx context.Context, data map[string]interface{}, ttl time.Duration, jwt bool) (*wrapping.ResponseWrapInfo, error) | 	ResponseWrapData(ctx context.Context, data map[string]interface{}, ttl time.Duration, jwt bool) (*wrapping.ResponseWrapInfo, error) | ||||||
| 	MlockEnabled() bool | 	MlockEnabled() bool | ||||||
| 	VaultVersion(ctx context.Context) (string, error) | 	VaultVersion(ctx context.Context) (string, error) | ||||||
|  | 	ClusterID(ctx context.Context) (string, error) | ||||||
| } | } | ||||||
|  |  | ||||||
| // LookRunnerUtil defines the functions for both Looker and Wrapper | // LookRunnerUtil defines the functions for both Looker and Wrapper | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user