Skip to content

Commit 9e449d9

Browse files
helenfufuthyton
authored andcommitted
add vault build date to system view plugin env VAULT-32676 (#29082)
--------- Co-authored-by: Thy Ton <maithytonn@gmail.com>
1 parent b169f0d commit 9e449d9

8 files changed

+142
-30
lines changed

changelog/29082.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
sdk: Add Vault build date to system view plugin environment response
3+
```

sdk/logical/plugin.pb.go

+39-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/logical/plugin.proto

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ syntax = "proto3";
55

66
package logical;
77

8+
import "google/protobuf/timestamp.proto";
9+
810
option go_package = "github.com/hashicorp/vault/sdk/logical";
911

1012
message PluginEnvironment {
@@ -16,4 +18,7 @@ message PluginEnvironment {
1618

1719
// VaultVersionMetadata is the version metadata of the Vault server
1820
string vault_version_metadata = 3;
21+
22+
// VaultBuildDate is the build date of the Vault server
23+
google.protobuf.Timestamp vault_build_date = 4;
1924
}

vault/dynamic_system_view.go

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/hashicorp/vault/sdk/logical"
1919
"github.com/hashicorp/vault/vault/plugincatalog"
2020
"github.com/hashicorp/vault/version"
21+
"google.golang.org/protobuf/types/known/timestamppb"
2122
)
2223

2324
type ctxKeyForwardedRequestMountAccessor struct{}
@@ -407,10 +408,17 @@ func (d dynamicSystemView) GroupsForEntity(entityID string) ([]*logical.Group, e
407408

408409
func (d dynamicSystemView) PluginEnv(_ context.Context) (*logical.PluginEnvironment, error) {
409410
v := version.GetVersion()
411+
412+
buildDate, err := version.GetVaultBuildDate()
413+
if err != nil {
414+
return nil, err
415+
}
416+
410417
return &logical.PluginEnvironment{
411418
VaultVersion: v.Version,
412419
VaultVersionPrerelease: v.VersionPrerelease,
413420
VaultVersionMetadata: v.VersionMetadata,
421+
VaultBuildDate: timestamppb.New(buildDate),
414422
}, nil
415423
}
416424

vault/dynamic_system_view_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/hashicorp/vault/helper/namespace"
1717
"github.com/hashicorp/vault/sdk/framework"
1818
"github.com/hashicorp/vault/sdk/logical"
19+
"github.com/hashicorp/vault/version"
20+
"google.golang.org/protobuf/types/known/timestamppb"
1921
)
2022

2123
var (
@@ -286,6 +288,50 @@ func TestDynamicSystemView_GeneratePasswordFromPolicy_failed(t *testing.T) {
286288
}
287289
}
288290

291+
// TestDynamicSystemView_PluginEnv_successful checks that the PluginEnv method returns the expected values in a successful case.
292+
func TestDynamicSystemView_PluginEnv_successful(t *testing.T) {
293+
coreConfig := &CoreConfig{
294+
CredentialBackends: map[string]logical.Factory{},
295+
}
296+
297+
cluster := NewTestCluster(t, coreConfig, &TestClusterOptions{})
298+
299+
cluster.Start()
300+
defer cluster.Cleanup()
301+
302+
core := cluster.Cores[0].Core
303+
TestWaitActive(t, core)
304+
305+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
306+
defer cancel()
307+
308+
ctx = namespace.RootContext(ctx)
309+
dsv := TestDynamicSystemView(cluster.Cores[0].Core, nil)
310+
311+
pluginEnv, err := dsv.PluginEnv(ctx)
312+
if err != nil {
313+
t.Fatalf("no error expected, but got: %s", err)
314+
}
315+
316+
expectedVersionInfo := version.GetVersion()
317+
318+
expectedBuildDate, err := version.GetVaultBuildDate()
319+
if err != nil {
320+
t.Fatalf("failed to set up expectedBuildDate: %v", err)
321+
}
322+
323+
expectedPluginEnv := &logical.PluginEnvironment{
324+
VaultVersion: expectedVersionInfo.Version,
325+
VaultVersionPrerelease: expectedVersionInfo.VersionPrerelease,
326+
VaultVersionMetadata: expectedVersionInfo.VersionMetadata,
327+
VaultBuildDate: timestamppb.New(expectedBuildDate),
328+
}
329+
330+
if !reflect.DeepEqual(pluginEnv, expectedPluginEnv) {
331+
t.Fatalf("got %q, expected %q", pluginEnv, expectedPluginEnv)
332+
}
333+
}
334+
289335
type runes []rune
290336

291337
func (r runes) Len() int { return len(r) }

vault/testing_util.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
// Copyright (c) HashiCorp, Inc.
22
// SPDX-License-Identifier: BUSL-1.1
33

4-
//go:build !enterprise
5-
64
package vault
75

86
import (
9-
"crypto/ed25519"
10-
"testing"
7+
"time"
8+
9+
"github.com/hashicorp/vault/version"
1110
)
1211

13-
func GenerateTestLicenseKeys() (ed25519.PublicKey, ed25519.PrivateKey, error) { return nil, nil, nil }
14-
func testGetLicensingConfig(key ed25519.PublicKey) *LicensingConfig { return &LicensingConfig{} }
15-
func testExtraTestCoreSetup(testing.TB, ed25519.PrivateKey, *TestClusterCore) {}
16-
func testAdjustUnderlyingStorage(tcc *TestClusterCore) {
17-
tcc.UnderlyingStorage = tcc.physical
12+
func init() {
13+
// The BuildDate is set as part of the build process in CI so we need to
14+
// initialize it for testing. By setting it to now minus one year we
15+
// provide some headroom to ensure that test license expiration (for enterprise)
16+
// does not exceed the BuildDate as that is invalid.
17+
if version.BuildDate == "" {
18+
version.BuildDate = time.Now().UTC().AddDate(-1, 0, 0).Format(time.RFC3339)
19+
}
1820
}
19-
func testApplyEntBaseConfig(coreConfig, base *CoreConfig) {}

vault/testing_util_stubs_oss.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
//go:build !enterprise
5+
6+
package vault
7+
8+
import (
9+
"crypto/ed25519"
10+
"testing"
11+
)
12+
13+
//go:generate go run github.com/hashicorp/vault/tools/stubmaker
14+
15+
func GenerateTestLicenseKeys() (ed25519.PublicKey, ed25519.PrivateKey, error) { return nil, nil, nil }
16+
func testGetLicensingConfig(key ed25519.PublicKey) *LicensingConfig { return &LicensingConfig{} }
17+
func testExtraTestCoreSetup(testing.TB, ed25519.PrivateKey, *TestClusterCore) {}
18+
func testAdjustUnderlyingStorage(tcc *TestClusterCore) {
19+
tcc.UnderlyingStorage = tcc.physical
20+
}
21+
func testApplyEntBaseConfig(coreConfig, base *CoreConfig) {}

version/version.go

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package version
66
import (
77
"bytes"
88
"fmt"
9+
"time"
910
)
1011

1112
type VersionInfo struct {
@@ -33,6 +34,14 @@ func GetVersion() *VersionInfo {
3334
}
3435
}
3536

37+
func GetVaultBuildDate() (time.Time, error) {
38+
buildDate, err := time.Parse(time.RFC3339, BuildDate)
39+
if err != nil {
40+
return time.Time{}, fmt.Errorf("failed to parse build date based on RFC3339: %w", err)
41+
}
42+
return buildDate, nil
43+
}
44+
3645
func (c *VersionInfo) VersionNumber() string {
3746
if Version == "unknown" && VersionPrerelease == "unknown" {
3847
return "(version unknown)"

0 commit comments

Comments
 (0)