From 17e011eea22819b364658a3252b0b560b1464281 Mon Sep 17 00:00:00 2001 From: Nick Cabatoff Date: Tue, 4 Jul 2023 16:08:09 -0400 Subject: [PATCH 01/10] Add a go:generate helper called stubmaker, which generates appropriate stubs on oss, and also on ent if the real func hasn't yet been defined. --- Makefile | 2 +- command/server/config_test_helpers.go | 4 + .../server/config_test_helpers_oss_stubs.go | 10 ++ command/server/config_test_helpers_util.go | 10 -- go.mod | 16 +- go.sum | 36 +++- tools/stubmaker/main.go | 158 ++++++++++++++++++ 7 files changed, 220 insertions(+), 16 deletions(-) create mode 100644 command/server/config_test_helpers_oss_stubs.go delete mode 100644 command/server/config_test_helpers_util.go create mode 100644 tools/stubmaker/main.go diff --git a/Makefile b/Makefile index 82fed7a83883..0800c8f8f17b 100644 --- a/Makefile +++ b/Makefile @@ -170,7 +170,7 @@ ci-lint: # source files. prep: fmtcheck @sh -c "'$(CURDIR)/scripts/goversioncheck.sh' '$(GO_VERSION_MIN)'" - @$(GO_CMD) generate $($(GO_CMD) list ./... | grep -v /vendor/) + @$(GO_CMD) generate $$($(GO_CMD) list ./... | grep -v /vendor/) @if [ -d .git/hooks ]; then cp .hooks/* .git/hooks/; fi # bootstrap the build by downloading additional tools needed to build diff --git a/command/server/config_test_helpers.go b/command/server/config_test_helpers.go index cc653af320c5..619c8e750a29 100644 --- a/command/server/config_test_helpers.go +++ b/command/server/config_test_helpers.go @@ -20,6 +20,10 @@ import ( "github.com/hashicorp/vault/internalshared/configutil" ) +//go:generate go run github.com/hashicorp/vault/tools/stubmaker "addExpectedEntConfig(c *Config, sentinelModules []string) {}" +//go:generate go run github.com/hashicorp/vault/tools/stubmaker "addExpectedDefaultEntConfig(c *Config) {}" +//go:generate go run github.com/hashicorp/vault/tools/stubmaker "addExpectedEntSanitizedConfig(c map[string]interface{}, sentinelModules []string) {}" + var DefaultCustomHeaders = map[string]map[string]string{ "default": { "Strict-Transport-Security": configutil.StrictTransportSecurity, diff --git a/command/server/config_test_helpers_oss_stubs.go b/command/server/config_test_helpers_oss_stubs.go new file mode 100644 index 000000000000..0307a47dd797 --- /dev/null +++ b/command/server/config_test_helpers_oss_stubs.go @@ -0,0 +1,10 @@ +//go:build !enterprise + +package server + +func addExpectedEntConfig(c *Config, sentinelModules []string) {} + +func addExpectedDefaultEntConfig(c *Config) {} + +func addExpectedEntSanitizedConfig(c map[string]interface{}, sentinelModules []string) {} + diff --git a/command/server/config_test_helpers_util.go b/command/server/config_test_helpers_util.go deleted file mode 100644 index 7f8c02e1c6a0..000000000000 --- a/command/server/config_test_helpers_util.go +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -//go:build !enterprise - -package server - -func addExpectedEntConfig(c *Config, sentinelModules []string) {} -func addExpectedDefaultEntConfig(c *Config) {} -func addExpectedEntSanitizedConfig(c map[string]interface{}, sentinelModules []string) {} diff --git a/go.mod b/go.mod index 846ba510f83f..d5c39463687c 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/Azure/go-autorest/autorest v0.11.29 github.com/Azure/go-autorest/autorest/adal v0.9.22 github.com/NYTimes/gziphandler v1.1.1 - github.com/ProtonMail/go-crypto v0.0.0-20220824120805-4b6e5c587895 + github.com/ProtonMail/go-crypto v0.0.0-20230518184743-7afd39499903 github.com/SAP/go-hdb v0.14.1 github.com/Sectorbob/mlab-ns2 v0.0.0-20171030222938-d3aa0c295a8a github.com/aerospike/aerospike-client-go/v5 v5.6.0 @@ -56,6 +56,7 @@ require ( github.com/favadi/protoc-go-inject-tag v1.4.0 github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 github.com/go-errors/errors v1.4.2 + github.com/go-git/go-git/v5 v5.7.0 github.com/go-jose/go-jose/v3 v3.0.0 github.com/go-ldap/ldap/v3 v3.4.4 github.com/go-sql-driver/mysql v1.6.0 @@ -264,6 +265,7 @@ require ( github.com/Masterminds/sprig/v3 v3.2.3 // indirect github.com/Microsoft/go-winio v0.6.1 // indirect github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect + github.com/acomagu/bufpipe v1.0.4 // indirect github.com/agext/levenshtein v1.2.1 // indirect github.com/andybalholm/brotli v1.0.4 // indirect github.com/apache/arrow/go/arrow v0.0.0-20211112161151-bc219186db40 // indirect @@ -297,7 +299,7 @@ require ( github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible // indirect github.com/circonus-labs/circonusllhist v0.1.3 // indirect - github.com/cloudflare/circl v1.1.0 // indirect + github.com/cloudflare/circl v1.3.3 // indirect github.com/cloudfoundry-community/go-cfclient v0.0.0-20210823134051-721f0e559306 // indirect github.com/cncf/udpa/go v0.0.0-20220112060539-c52dc94e7fbe // indirect github.com/cncf/xds/go v0.0.0-20230310173818-32f1caf87195 // indirect @@ -324,6 +326,7 @@ require ( github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect github.com/dvsekhvalnov/jose2go v1.5.0 // indirect github.com/emicklei/go-restful/v3 v3.10.1 // indirect + github.com/emirpasic/gods v1.18.1 // indirect github.com/envoyproxy/go-control-plane v0.11.0 // indirect github.com/envoyproxy/protoc-gen-validate v0.10.0 // indirect github.com/evanphx/json-patch/v5 v5.6.0 // indirect @@ -333,6 +336,8 @@ require ( github.com/gammazero/deque v0.2.1 // indirect github.com/gammazero/workerpool v1.1.3 // indirect github.com/go-asn1-ber/asn1-ber v1.5.4 // indirect + github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect + github.com/go-git/go-billy/v5 v5.4.1 // indirect github.com/go-ldap/ldif v0.0.0-20200320164324-fd88d9b715b3 // indirect github.com/go-logr/logr v1.2.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect @@ -392,6 +397,7 @@ require ( github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/pgtype v1.14.0 // indirect github.com/jackc/pgx v3.3.0+incompatible // indirect + github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect github.com/jcmturner/aescts/v2 v2.0.0 // indirect github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect github.com/jcmturner/gofork v1.7.6 // indirect @@ -402,6 +408,7 @@ require ( github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/kelseyhightower/envconfig v1.4.0 // indirect + github.com/kevinburke/ssh_config v1.2.0 // indirect github.com/klauspost/compress v1.16.5 // indirect github.com/klauspost/pgzip v1.2.5 // indirect github.com/kylelemons/godebug v1.1.0 // indirect @@ -436,6 +443,7 @@ require ( github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect github.com/pierrec/lz4 v2.6.1+incompatible // indirect github.com/pierrec/lz4/v4 v4.1.17 // indirect + github.com/pjbgf/sha1cd v0.3.0 // indirect github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect @@ -444,8 +452,10 @@ require ( github.com/prometheus/procfs v0.8.0 // indirect github.com/renier/xmlrpc v0.0.0-20170708154548-ce4a1a486c03 // indirect github.com/rogpeppe/go-internal v1.10.0 // indirect + github.com/sergi/go-diff v1.1.0 // indirect github.com/shopspring/decimal v1.3.1 // indirect github.com/sirupsen/logrus v1.9.0 // indirect + github.com/skeema/knownhosts v1.1.1 // indirect github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 // indirect github.com/snowflakedb/gosnowflake v1.6.18 // indirect github.com/softlayer/softlayer-go v0.0.0-20180806151055-260589d94c7d // indirect @@ -460,6 +470,7 @@ require ( github.com/tv42/httpunix v0.0.0-20191220191345-2ba4b9c3382c // indirect github.com/ulikunitz/xz v0.5.10 // indirect github.com/vmware/govmomi v0.18.0 // indirect + github.com/xanzy/ssh-agent v0.3.3 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/scram v1.1.1 // indirect github.com/xdg-go/stringprep v1.0.3 // indirect @@ -486,6 +497,7 @@ require ( gopkg.in/jcmturner/goidentity.v3 v3.0.0 // indirect gopkg.in/resty.v1 v1.12.0 // indirect gopkg.in/square/go-jose.v2 v2.6.0 // indirect + gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/api v0.27.2 // indirect diff --git a/go.sum b/go.sum index 9684566811bc..27b40d16d1c6 100644 --- a/go.sum +++ b/go.sum @@ -684,8 +684,8 @@ github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEV github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/OneOfOne/xxhash v1.2.8/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q= -github.com/ProtonMail/go-crypto v0.0.0-20220824120805-4b6e5c587895 h1:NsReiLpErIPzRrnogAXYwSoU7txA977LjDGrbkewJbg= -github.com/ProtonMail/go-crypto v0.0.0-20220824120805-4b6e5c587895/go.mod h1:UBYPn8k0D56RtnR8RFQMjmh4KrZzWJ5o7Z9SYjossQ8= +github.com/ProtonMail/go-crypto v0.0.0-20230518184743-7afd39499903 h1:ZK3C5DtzV2nVAQTx5S5jQvMeDqWtD1By5mOoyY/xJek= +github.com/ProtonMail/go-crypto v0.0.0-20230518184743-7afd39499903/go.mod h1:8TI4H3IbrackdNgv+92dI+rhpCaLqM0IfpgCgenFvRE= github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= @@ -698,6 +698,8 @@ github.com/Sectorbob/mlab-ns2 v0.0.0-20171030222938-d3aa0c295a8a/go.mod h1:D73UA github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ= github.com/abdullin/seq v0.0.0-20160510034733-d5467c17e7af h1:DBNMBMuMiWYu0b+8KMJuWmfCkcxl09JwdlqwDZZ6U14= github.com/abdullin/seq v0.0.0-20160510034733-d5467c17e7af/go.mod h1:5Jv4cbFiHJMsVxt52+i0Ha45fjshj6wxYr1r19tB9bw= +github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ= +github.com/acomagu/bufpipe v1.0.4/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4= github.com/aerospike/aerospike-client-go/v5 v5.6.0 h1:tRxcUq0HY8fFPQEzF3EgrknF+w1xFO0YDfUb9Nm8yRI= github.com/aerospike/aerospike-client-go/v5 v5.6.0/go.mod h1:rJ/KpmClE7kiBPfvAPrGw9WuNOiz8v2uKbQaUyYPXtI= github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8= @@ -723,6 +725,7 @@ github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220418222510-f25a4f6275ed/go.mod h1:F7bn7fEU90QkQ3tnmaTx3LTKLEDqnwWODIYppRQ5hnY= github.com/antlr/antlr4/runtime/Go/antlr v1.4.10/go.mod h1:F7bn7fEU90QkQ3tnmaTx3LTKLEDqnwWODIYppRQ5hnY= @@ -745,6 +748,7 @@ github.com/armon/go-metrics v0.4.1/go.mod h1:E6amYzXo6aW1tqzoZGT755KkbgrJsSdpwZ+ github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI= github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= @@ -899,8 +903,9 @@ github.com/circonus-labs/circonusllhist v0.1.3 h1:TJH+oke8D16535+jHExHj4nQvzlZrj github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I= github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= -github.com/cloudflare/circl v1.1.0 h1:bZgT/A+cikZnKIwn7xL2OBj012Bmvho/o6RpRvv3GKY= github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtMxxK7fi4I= +github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs= +github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= github.com/cloudfoundry-community/go-cfclient v0.0.0-20210823134051-721f0e559306 h1:k8q2Nsz7kNaUlysVCnWIFLMUSqiKXaGLdIf9P0GsX2Y= github.com/cloudfoundry-community/go-cfclient v0.0.0-20210823134051-721f0e559306/go.mod h1:0FdHblxw7g3M2PPICOw9i8YZOHP9dZTHbJUtoxL7Z/E= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= @@ -1179,12 +1184,15 @@ github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25Kn github.com/dvsekhvalnov/jose2go v1.5.0 h1:3j8ya4Z4kMCwT5nXIKFSV84YS+HdqSSO0VsTQxaLAeM= github.com/dvsekhvalnov/jose2go v1.5.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU= github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc= +github.com/elazarl/goproxy v0.0.0-20221015165544-a0805db90819 h1:RIB4cRk+lBqKK3Oy0r2gRX4ui7tuhiZq2SuTtTCi0/0= github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs= github.com/emicklei/go-restful v2.9.5+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs= github.com/emicklei/go-restful/v3 v3.8.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= github.com/emicklei/go-restful/v3 v3.9.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= github.com/emicklei/go-restful/v3 v3.10.1 h1:rc42Y5YTp7Am7CS630D7JmhRjq4UlEUuEKfrDac4bSQ= github.com/emicklei/go-restful/v3 v3.10.1/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= +github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= +github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= @@ -1258,6 +1266,7 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14= github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= +github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY= github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q= github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q= github.com/go-asn1-ber/asn1-ber v1.3.1/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0= @@ -1272,6 +1281,13 @@ github.com/go-fonts/latin-modern v0.2.0/go.mod h1:rQVLdDMK+mK1xscDwsqM5J8U2jrRa3 github.com/go-fonts/liberation v0.1.1/go.mod h1:K6qoJYypsmfVjWg8KOVDQhLc8UDgIK2HYqyqAO9z7GY= github.com/go-fonts/liberation v0.2.0/go.mod h1:K6qoJYypsmfVjWg8KOVDQhLc8UDgIK2HYqyqAO9z7GY= github.com/go-fonts/stix v0.1.0/go.mod h1:w/c1f0ldAUlJmLBvlbkvVXLAD+tAMqobIIQpmnUIzUY= +github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= +github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= +github.com/go-git/go-billy/v5 v5.4.1 h1:Uwp5tDRkPr+l/TnbHOQzp+tmJfLceOlbVucgpTz8ix4= +github.com/go-git/go-billy/v5 v5.4.1/go.mod h1:vjbugF6Fz7JIflbVpl1hJsGjSHNltrSw45YK/ukIvQg= +github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20230305113008-0c11038e723f h1:Pz0DHeFij3XFhoBRGUDPzSJ+w2UcK5/0JvF8DRI58r8= +github.com/go-git/go-git/v5 v5.7.0 h1:t9AudWVLmqzlo+4bqdf7GY+46SUuRsx59SboFxkq2aE= +github.com/go-git/go-git/v5 v5.7.0/go.mod h1:coJHKEOk5kUClpsNlXrUvPrDxY3w3gjHvhcZd8Fodw8= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= @@ -2004,6 +2020,8 @@ github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dv github.com/jackc/puddle v1.3.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jarcoal/httpmock v0.0.0-20180424175123-9c70cfe4a1da/go.mod h1:ks+b9deReOc7jgqp+e7LuFiCBH6Rm5hL32cLcEAArb4= github.com/jarcoal/httpmock v1.0.7 h1:d1a2VFpSdm5gtjhCPWsQHSnx8+5V3ms5431YwvmkuNk= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jcmturner/aescts/v2 v2.0.0 h1:9YKLH6ey7H4eDBXW8khjYslgyqG2xZikXP0EQFKrle8= github.com/jcmturner/aescts/v2 v2.0.0/go.mod h1:AiaICIRyfYg35RUkr8yESTqvSy7csK90qZ5xfvvsoNs= github.com/jcmturner/dnsutils/v2 v2.0.0 h1:lltnkeZGL0wILNvrNiVCR6Ro5PGU/SeBvVO/8c/iPbo= @@ -2065,6 +2083,8 @@ github.com/karrick/godirwalk v1.10.3/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0Lh github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= +github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= +github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= @@ -2149,6 +2169,8 @@ github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kN github.com/marstr/guid v1.1.0/go.mod h1:74gB1z2wpxxInTG6yaqA7KrtM0NZ+RbrcqDvYHefzho= github.com/martini-contrib/render v0.0.0-20150707142108-ec18f8345a11 h1:YFh+sjyJTMQSYjKwM4dFKhJPJC/wfo98tPUc17HdoYw= github.com/martini-contrib/render v0.0.0-20150707142108-ec18f8345a11/go.mod h1:Ah2dBMoxZEqk118as2T4u4fjfXarE0pPnMJaArZQZsI= +github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A= +github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= @@ -2445,6 +2467,8 @@ github.com/pierrec/lz4/v4 v4.1.17 h1:kV4Ip+/hUBC+8T6+2EgburRtkE9ef4nbY3f4dFhGjMc github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/pires/go-proxyproto v0.6.1 h1:EBupykFmo22SDjv4fQVQd2J9NOoLPmyZA/15ldOGkPw= github.com/pires/go-proxyproto v0.6.1/go.mod h1:Odh9VFOZJCf9G8cLW5o435Xf1J95Jw9Gw5rnCjcwzAY= +github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= +github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI= github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI= @@ -2593,6 +2617,8 @@ github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/skeema/knownhosts v1.1.1 h1:MTk78x9FPgDFVFkDLTrsnnfCJl7g1C/nnKvePgrIngE= +github.com/skeema/knownhosts v1.1.1/go.mod h1:g4fPeYpque7P0xefxtGzV81ihjC8sX2IqpAoNkjxbMo= github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA= github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= @@ -2727,6 +2753,8 @@ github.com/vmware/govmomi v0.18.0/go.mod h1:URlwyTFZX72RmxtxuaFL2Uj3fD1JTvZdx59b github.com/willf/bitset v1.1.11-0.20200630133818-d5bec3311243/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4= github.com/willf/bitset v1.1.11/go.mod h1:83CECat5yLh5zVOf4P1ErAgKA5UDvKtgyUABdr3+MjI= github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= +github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= +github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= github.com/xdg-go/scram v1.1.1 h1:VOMT+81stJgXW3CpHyqHN3AXDYIMsx56mEFrB37Mb/E= @@ -3805,6 +3833,8 @@ gopkg.in/square/go-jose.v2 v2.6.0 h1:NGk74WTnPKBNUhNzQX7PYcTLUjoq7mzKk2OKbvwk2iI gopkg.in/square/go-jose.v2 v2.6.0/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= +gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/tools/stubmaker/main.go b/tools/stubmaker/main.go new file mode 100644 index 000000000000..fb518e249791 --- /dev/null +++ b/tools/stubmaker/main.go @@ -0,0 +1,158 @@ +package main + +import ( + "bufio" + "errors" + "fmt" + "go/types" + "os" + "strings" + + "github.com/go-git/go-git/v5" + "golang.org/x/tools/go/packages" +) + +func main() { + baseFilename := strings.TrimSuffix(os.Getenv("GOFILE"), ".go") + packageName := os.Getenv("GOPACKAGE") + stub := os.Args[1] + repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{ + DetectDotGit: true, + }) + if err != nil { + fatal(err) + } + wt, err := repo.Worktree() + if err != nil { + fatal(err) + } + + st, err := wt.Filesystem.Stat("enthelpers") + onOss := errors.Is(err, os.ErrNotExist) + onEnt := st != nil + var repoString string + var tags []string + switch { + case onOss && !onEnt: + repoString, tags = "oss", nil + case !onOss && onEnt: + repoString, tags = "ent", []string{"enterprise"} + default: + fatal(err) + } + pkg, err := parsePackage(".", tags) + if err != nil { + fatal(err) + } + //for k, v := range pkg.TypesInfo.Defs { + // if v != nil { + // _, ok := v.Type().(*types.Signature) + // if ok { + // fmt.Println(k, v.String()) + // } + // } + //} + target := fmt.Sprintf("%s_%s_stubs.go", baseFilename, repoString) + appendStub(target, packageName, stub, onOss, pkg) + // fmt.Println(target, packageName, stub, wt.Filesystem.Root(), onOss, onEnt) +} + +func appendStub(target string, packageName string, stub string, onOss bool, pkg *packages.Package) error { + _, err := os.Stat(target) + var f *os.File + var lines []string + if err == nil { + f, err = os.OpenFile(target, os.O_RDWR, 0o644) + if err != nil { + fatal(err) + } + scanner := bufio.NewScanner(f) + scanner.Split(bufio.ScanLines) + + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + } else { + if !errors.Is(err, os.ErrNotExist) { + return err + } + f, err = os.Create(target) + if err != nil { + return err + } + defer func() { + st, _ := f.Stat() + if st.Size() == 0 { + os.Remove(target) + } + }() + } + + // TODO add edit warning + var add []string + if onOss { + add = append(add, "//go:build !enterprise") + } else { + parenPos := strings.Index(stub, "(") + if parenPos == -1 { + fatal(fmt.Errorf("no paren found")) + } + funcName := strings.TrimSpace(stub[:parenPos]) + for name, val := range pkg.TypesInfo.Defs { + if val == nil { + continue + } + _, ok := val.Type().(*types.Signature) + if ok && name.Name == funcName { + //if val.String() != "func "+stub { + // fatal(fmt.Errorf("found existing def with different sig: %s", val.String())) + //} + return nil + } + } + } + add = append(add, fmt.Sprintf("package %s", packageName)) + add = append(add, "func "+stub) + + for _, line := range add { + if stringPresent(lines, line) { + continue + } + _, err = f.WriteString(line + "\n\n") + if err != nil { + return err + } + } + + return nil +} + +func stringPresent(haystack []string, needle string) bool { + for _, line := range haystack { + if strings.Contains(line, needle) { + return true + } + } + return false +} + +func fatal(err error) { + fmt.Println(err) + os.Exit(1) +} + +func parsePackage(name string, tags []string) (*packages.Package, error) { + cfg := &packages.Config{ + Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax, + Tests: false, + BuildFlags: []string{fmt.Sprintf("-tags=%s", strings.Join(tags, " "))}, + } + pkgs, err := packages.Load(cfg, name) + if err != nil { + return nil, err + } + if len(pkgs) != 1 { + return nil, fmt.Errorf("error: %d packages found", len(pkgs)) + } + return pkgs[0], nil +} From fb571caa3285d0f4e921c0204c013d5e4325d9d1 Mon Sep 17 00:00:00 2001 From: Nick Cabatoff Date: Tue, 4 Jul 2023 16:47:07 -0400 Subject: [PATCH 02/10] Some cleanup, plus add copyright lines and generated message to outputs. --- .../server/config_test_helpers_oss_stubs.go | 5 ++ tools/stubmaker/main.go | 85 +++++++++++-------- 2 files changed, 55 insertions(+), 35 deletions(-) diff --git a/command/server/config_test_helpers_oss_stubs.go b/command/server/config_test_helpers_oss_stubs.go index 0307a47dd797..e045449f4daf 100644 --- a/command/server/config_test_helpers_oss_stubs.go +++ b/command/server/config_test_helpers_oss_stubs.go @@ -1,3 +1,8 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by tools/stubmaker; DO NOT EDIT. + //go:build !enterprise package server diff --git a/tools/stubmaker/main.go b/tools/stubmaker/main.go index fb518e249791..34d66743caa0 100644 --- a/tools/stubmaker/main.go +++ b/tools/stubmaker/main.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "go/types" + "log" "os" "strings" @@ -16,20 +17,31 @@ func main() { baseFilename := strings.TrimSuffix(os.Getenv("GOFILE"), ".go") packageName := os.Getenv("GOPACKAGE") stub := os.Args[1] + enterprise, repoString, tags := repoDetails() + pkg, err := parsePackage(".", tags) + if err != nil { + log.Fatal(err) + } + target := fmt.Sprintf("%s_%s_stubs.go", baseFilename, repoString) + appendStub(target, packageName, stub, enterprise, pkg) +} + +func repoDetails() (bool, string, []string) { repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{ DetectDotGit: true, }) if err != nil { - fatal(err) + log.Fatal(err) } wt, err := repo.Worktree() if err != nil { - fatal(err) + log.Fatal(err) } st, err := wt.Filesystem.Stat("enthelpers") onOss := errors.Is(err, os.ErrNotExist) onEnt := st != nil + var repoString string var tags []string switch { @@ -38,33 +50,29 @@ func main() { case !onOss && onEnt: repoString, tags = "ent", []string{"enterprise"} default: - fatal(err) + log.Fatal(err) } - pkg, err := parsePackage(".", tags) - if err != nil { - fatal(err) - } - //for k, v := range pkg.TypesInfo.Defs { - // if v != nil { - // _, ok := v.Type().(*types.Signature) - // if ok { - // fmt.Println(k, v.String()) - // } - // } - //} - target := fmt.Sprintf("%s_%s_stubs.go", baseFilename, repoString) - appendStub(target, packageName, stub, onOss, pkg) - // fmt.Println(target, packageName, stub, wt.Filesystem.Root(), onOss, onEnt) + return onEnt, repoString, tags } -func appendStub(target string, packageName string, stub string, onOss bool, pkg *packages.Package) error { - _, err := os.Stat(target) +func appendStub(target string, packageName string, stub string, enterprise bool, pkg *packages.Package) (err error) { + prelude := `// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by tools/stubmaker; DO NOT EDIT. + +` + + var added bool + + // Open an existing stub file and read it, or create it if it doesn't exist. + _, err = os.Stat(target) var f *os.File var lines []string if err == nil { f, err = os.OpenFile(target, os.O_RDWR, 0o644) if err != nil { - fatal(err) + log.Fatal(err) } scanner := bufio.NewScanner(f) scanner.Split(bufio.ScanLines) @@ -76,26 +84,30 @@ func appendStub(target string, packageName string, stub string, onOss bool, pkg if !errors.Is(err, os.ErrNotExist) { return err } - f, err = os.Create(target) + f, err = os.Create(target + ".tmp") if err != nil { return err } + // If we don't end up writing to the file, delete it. defer func() { - st, _ := f.Stat() - if st.Size() == 0 { + if err != nil || !added { os.Remove(target) + } else { + os.Rename(target+".tmp", target) } }() + + _, err = f.WriteString(prelude) + if err != nil { + return err + } } - // TODO add edit warning var add []string - if onOss { - add = append(add, "//go:build !enterprise") - } else { + if enterprise { parenPos := strings.Index(stub, "(") if parenPos == -1 { - fatal(fmt.Errorf("no paren found")) + log.Fatal(fmt.Errorf("no paren found")) } funcName := strings.TrimSpace(stub[:parenPos]) for name, val := range pkg.TypesInfo.Defs { @@ -104,17 +116,24 @@ func appendStub(target string, packageName string, stub string, onOss bool, pkg } _, ok := val.Type().(*types.Signature) if ok && name.Name == funcName { + // TODO would be nice to check the signature, will be more work + // since String() renders the sinagture with fully qualified type + // names (i.e. including the full package). //if val.String() != "func "+stub { - // fatal(fmt.Errorf("found existing def with different sig: %s", val.String())) + // log.Fatal(fmt.Errorf("found existing def with different sig: %s", val.String())) //} return nil } } + } else { + add = append(add, "//go:build !enterprise") } add = append(add, fmt.Sprintf("package %s", packageName)) add = append(add, "func "+stub) for _, line := range add { + // TODO should we instead just rely on the same thing we do above, + // using the types parsed by packages? if stringPresent(lines, line) { continue } @@ -122,6 +141,7 @@ func appendStub(target string, packageName string, stub string, onOss bool, pkg if err != nil { return err } + added = true } return nil @@ -136,11 +156,6 @@ func stringPresent(haystack []string, needle string) bool { return false } -func fatal(err error) { - fmt.Println(err) - os.Exit(1) -} - func parsePackage(name string, tags []string) (*packages.Package, error) { cfg := &packages.Config{ Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax, From 0290d7bced6fbf073bbce014af459dffac7ed05c Mon Sep 17 00:00:00 2001 From: Nick Cabatoff Date: Wed, 5 Jul 2023 08:20:56 -0400 Subject: [PATCH 03/10] Fix rm --- tools/stubmaker/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/stubmaker/main.go b/tools/stubmaker/main.go index 34d66743caa0..9233e37a67ff 100644 --- a/tools/stubmaker/main.go +++ b/tools/stubmaker/main.go @@ -91,7 +91,7 @@ func appendStub(target string, packageName string, stub string, enterprise bool, // If we don't end up writing to the file, delete it. defer func() { if err != nil || !added { - os.Remove(target) + os.Remove(target + ".tmp") } else { os.Rename(target+".tmp", target) } From e7c3ee44f0571da6a78a9ae8e221cc11563e1d2f Mon Sep 17 00:00:00 2001 From: Nick Cabatoff Date: Wed, 5 Jul 2023 08:24:13 -0400 Subject: [PATCH 04/10] Test new stub --- vault/teststubmaker.go | 7 +++++++ vault/teststubmaker_oss_stubs.go | 11 +++++++++++ 2 files changed, 18 insertions(+) create mode 100644 vault/teststubmaker.go create mode 100644 vault/teststubmaker_oss_stubs.go diff --git a/vault/teststubmaker.go b/vault/teststubmaker.go new file mode 100644 index 000000000000..e1eec72b77a9 --- /dev/null +++ b/vault/teststubmaker.go @@ -0,0 +1,7 @@ +package vault + +//go:generate go run github.com/hashicorp/vault/tools/stubmaker "myfunc() {}" + +func testmyfunc() { + myfunc() +} diff --git a/vault/teststubmaker_oss_stubs.go b/vault/teststubmaker_oss_stubs.go new file mode 100644 index 000000000000..5fb780140e10 --- /dev/null +++ b/vault/teststubmaker_oss_stubs.go @@ -0,0 +1,11 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by tools/stubmaker; DO NOT EDIT. + +//go:build !enterprise + +package vault + +func myfunc() {} + From 5befd1caeca3101b7fd540b044e08eef9c58e038 Mon Sep 17 00:00:00 2001 From: Nick Cabatoff Date: Wed, 5 Jul 2023 14:49:09 -0400 Subject: [PATCH 05/10] New approach, basically just copy and munge the input stubs file. --- command/server/config_test_helpers.go | 4 - .../server/config_test_helpers_oss_stubs.go | 15 -- .../server/config_test_helpers_stubs_oss.go | 12 ++ tools/stubmaker/main.go | 187 ++++++++---------- vault/teststubmaker.go | 4 +- vault/teststubmaker_oss.go | 15 ++ vault/teststubmaker_oss_stubs.go | 11 -- 7 files changed, 116 insertions(+), 132 deletions(-) delete mode 100644 command/server/config_test_helpers_oss_stubs.go create mode 100644 command/server/config_test_helpers_stubs_oss.go create mode 100644 vault/teststubmaker_oss.go delete mode 100644 vault/teststubmaker_oss_stubs.go diff --git a/command/server/config_test_helpers.go b/command/server/config_test_helpers.go index 619c8e750a29..cc653af320c5 100644 --- a/command/server/config_test_helpers.go +++ b/command/server/config_test_helpers.go @@ -20,10 +20,6 @@ import ( "github.com/hashicorp/vault/internalshared/configutil" ) -//go:generate go run github.com/hashicorp/vault/tools/stubmaker "addExpectedEntConfig(c *Config, sentinelModules []string) {}" -//go:generate go run github.com/hashicorp/vault/tools/stubmaker "addExpectedDefaultEntConfig(c *Config) {}" -//go:generate go run github.com/hashicorp/vault/tools/stubmaker "addExpectedEntSanitizedConfig(c map[string]interface{}, sentinelModules []string) {}" - var DefaultCustomHeaders = map[string]map[string]string{ "default": { "Strict-Transport-Security": configutil.StrictTransportSecurity, diff --git a/command/server/config_test_helpers_oss_stubs.go b/command/server/config_test_helpers_oss_stubs.go deleted file mode 100644 index e045449f4daf..000000000000 --- a/command/server/config_test_helpers_oss_stubs.go +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -// Code generated by tools/stubmaker; DO NOT EDIT. - -//go:build !enterprise - -package server - -func addExpectedEntConfig(c *Config, sentinelModules []string) {} - -func addExpectedDefaultEntConfig(c *Config) {} - -func addExpectedEntSanitizedConfig(c map[string]interface{}, sentinelModules []string) {} - diff --git a/command/server/config_test_helpers_stubs_oss.go b/command/server/config_test_helpers_stubs_oss.go new file mode 100644 index 000000000000..6c3ab18bcffd --- /dev/null +++ b/command/server/config_test_helpers_stubs_oss.go @@ -0,0 +1,12 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +//go:build !enterprise + +package server + +//go:generate go run github.com/hashicorp/vault/tools/stubmaker + +func addExpectedEntConfig(c *Config, sentinelModules []string) {} +func addExpectedDefaultEntConfig(c *Config) {} +func addExpectedEntSanitizedConfig(c map[string]interface{}, sentinelModules []string) {} diff --git a/tools/stubmaker/main.go b/tools/stubmaker/main.go index 9233e37a67ff..5bcd61de1d5d 100644 --- a/tools/stubmaker/main.go +++ b/tools/stubmaker/main.go @@ -2,9 +2,11 @@ package main import ( "bufio" + "bytes" "errors" "fmt" "go/types" + "io" "log" "os" "strings" @@ -13,20 +15,7 @@ import ( "golang.org/x/tools/go/packages" ) -func main() { - baseFilename := strings.TrimSuffix(os.Getenv("GOFILE"), ".go") - packageName := os.Getenv("GOPACKAGE") - stub := os.Args[1] - enterprise, repoString, tags := repoDetails() - pkg, err := parsePackage(".", tags) - if err != nil { - log.Fatal(err) - } - target := fmt.Sprintf("%s_%s_stubs.go", baseFilename, repoString) - appendStub(target, packageName, stub, enterprise, pkg) -} - -func repoDetails() (bool, string, []string) { +func isEnterprise() bool { repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{ DetectDotGit: true, }) @@ -42,118 +31,116 @@ func repoDetails() (bool, string, []string) { onOss := errors.Is(err, os.ErrNotExist) onEnt := st != nil - var repoString string - var tags []string switch { case onOss && !onEnt: - repoString, tags = "oss", nil case !onOss && onEnt: - repoString, tags = "ent", []string{"enterprise"} default: log.Fatal(err) } - return onEnt, repoString, tags + return onEnt } -func appendStub(target string, packageName string, stub string, enterprise bool, pkg *packages.Package) (err error) { - prelude := `// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -// Code generated by tools/stubmaker; DO NOT EDIT. - -` +func main() { + if !isEnterprise() { + return + } + baseFilename := strings.TrimSuffix(os.Getenv("GOFILE"), "_oss.go") + if err := writeStubIfNeeded(baseFilename); err != nil { + log.Fatal(err) + } +} - var added bool +func readLines(r io.Reader) ([]string, error) { + scanner := bufio.NewScanner(r) + scanner.Split(bufio.ScanLines) - // Open an existing stub file and read it, or create it if it doesn't exist. - _, err = os.Stat(target) - var f *os.File var lines []string - if err == nil { - f, err = os.OpenFile(target, os.O_RDWR, 0o644) - if err != nil { - log.Fatal(err) - } - scanner := bufio.NewScanner(f) - scanner.Split(bufio.ScanLines) + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + if err := scanner.Err(); err != nil { + return nil, err + } + return lines, nil +} - for scanner.Scan() { - lines = append(lines, scanner.Text()) - } - } else { - if !errors.Is(err, os.ErrNotExist) { - return err - } - f, err = os.Create(target + ".tmp") - if err != nil { - return err - } - // If we don't end up writing to the file, delete it. - defer func() { - if err != nil || !added { - os.Remove(target + ".tmp") - } else { - os.Rename(target+".tmp", target) - } - }() +func writeStubIfNeeded(baseFilename string) (err error) { + warning := "// Code generated by tools/stubmaker; DO NOT EDIT." - _, err = f.WriteString(prelude) - if err != nil { - return err - } + var output *os.File + inputFile := fmt.Sprintf("%s_oss.go", baseFilename) + b, err := os.ReadFile(inputFile) + if err != nil { + return err } - var add []string - if enterprise { - parenPos := strings.Index(stub, "(") - if parenPos == -1 { - log.Fatal(fmt.Errorf("no paren found")) + inputLines, err := readLines(bytes.NewBuffer(b)) + var funcs []string + var outputLines []string + for _, line := range inputLines { + switch line { + case "//go:build !enterprise": + outputLines = append(outputLines, warning, "") + line = "//go:build enterprise" + case "//go:generate go run github.com/hashicorp/vault/tools/stubmaker": + continue } - funcName := strings.TrimSpace(stub[:parenPos]) - for name, val := range pkg.TypesInfo.Defs { - if val == nil { - continue - } - _, ok := val.Type().(*types.Signature) - if ok && name.Name == funcName { - // TODO would be nice to check the signature, will be more work - // since String() renders the sinagture with fully qualified type - // names (i.e. including the full package). - //if val.String() != "func "+stub { - // log.Fatal(fmt.Errorf("found existing def with different sig: %s", val.String())) - //} - return nil + outputLines = append(outputLines, line) + + trimmed := strings.TrimSpace(line) + if strings.HasPrefix(trimmed, "func ") { + i := strings.Index(trimmed, "(") + if i != -1 { + funcs = append(funcs, trimmed[5:i]) } } - } else { - add = append(add, "//go:build !enterprise") } - add = append(add, fmt.Sprintf("package %s", packageName)) - add = append(add, "func "+stub) - for _, line := range add { - // TODO should we instead just rely on the same thing we do above, - // using the types parsed by packages? - if stringPresent(lines, line) { + target := fmt.Sprintf("%s_ent.go", baseFilename) + os.Remove(target) + + pkg, err := parsePackage(".", []string{"enterprise"}) + if err != nil { + log.Fatal(err) + } + + var found []string + for name, val := range pkg.TypesInfo.Defs { + if val == nil { continue } - _, err = f.WriteString(line + "\n\n") - if err != nil { - return err + _, ok := val.Type().(*types.Signature) + if !ok { + continue + } + for _, f := range funcs { + if name.Name == f { + found = append(found, f) + } } - added = true + } + switch { + case len(found) == len(funcs): + return + case len(found) != 0: + log.Fatalf("funcs partially defined: need=%v, found=%v", funcs, found) } - return nil -} - -func stringPresent(haystack []string, needle string) bool { - for _, line := range haystack { - if strings.Contains(line, needle) { - return true - } + output, err = os.Create(target + ".tmp") + if err != nil { + return err } - return false + // If we don't end up writing to the file, delete it. + defer func() { + if err != nil { + os.Remove(target + ".tmp") + } else { + os.Rename(target+".tmp", target) + } + }() + + _, err = io.WriteString(output, strings.Join(outputLines, "\n")) + return err } func parsePackage(name string, tags []string) (*packages.Package, error) { diff --git a/vault/teststubmaker.go b/vault/teststubmaker.go index e1eec72b77a9..c221d777a609 100644 --- a/vault/teststubmaker.go +++ b/vault/teststubmaker.go @@ -1,7 +1,7 @@ package vault -//go:generate go run github.com/hashicorp/vault/tools/stubmaker "myfunc() {}" +import "github.com/hashicorp/vault/vault/cluster" func testmyfunc() { - myfunc() + myfunc(cluster.Listener{}) } diff --git a/vault/teststubmaker_oss.go b/vault/teststubmaker_oss.go new file mode 100644 index 000000000000..be64cbf4e5a0 --- /dev/null +++ b/vault/teststubmaker_oss.go @@ -0,0 +1,15 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +//go:build !enterprise + +package vault + +import ( + "github.com/hashicorp/vault/vault/cluster" + "github.com/hashicorp/vault/vault/seal" +) + +//go:generate go run github.com/hashicorp/vault/tools/stubmaker +func myfunc(cluster.Listener) *seal.Envelope { return nil } +func myfunc2() *seal.Envelope { return nil } diff --git a/vault/teststubmaker_oss_stubs.go b/vault/teststubmaker_oss_stubs.go deleted file mode 100644 index 5fb780140e10..000000000000 --- a/vault/teststubmaker_oss_stubs.go +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -// Code generated by tools/stubmaker; DO NOT EDIT. - -//go:build !enterprise - -package vault - -func myfunc() {} - From 7c7f3954ad7b79842ef8f92df543b1438d804f8e Mon Sep 17 00:00:00 2001 From: Nick Cabatoff Date: Wed, 5 Jul 2023 15:22:29 -0400 Subject: [PATCH 06/10] Cleanup --- ...rs_stubs_oss.go => config_test_helpers_oss.go} | 0 vault/teststubmaker.go | 7 ------- vault/teststubmaker_oss.go | 15 --------------- 3 files changed, 22 deletions(-) rename command/server/{config_test_helpers_stubs_oss.go => config_test_helpers_oss.go} (100%) delete mode 100644 vault/teststubmaker.go delete mode 100644 vault/teststubmaker_oss.go diff --git a/command/server/config_test_helpers_stubs_oss.go b/command/server/config_test_helpers_oss.go similarity index 100% rename from command/server/config_test_helpers_stubs_oss.go rename to command/server/config_test_helpers_oss.go diff --git a/vault/teststubmaker.go b/vault/teststubmaker.go deleted file mode 100644 index c221d777a609..000000000000 --- a/vault/teststubmaker.go +++ /dev/null @@ -1,7 +0,0 @@ -package vault - -import "github.com/hashicorp/vault/vault/cluster" - -func testmyfunc() { - myfunc(cluster.Listener{}) -} diff --git a/vault/teststubmaker_oss.go b/vault/teststubmaker_oss.go deleted file mode 100644 index be64cbf4e5a0..000000000000 --- a/vault/teststubmaker_oss.go +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -//go:build !enterprise - -package vault - -import ( - "github.com/hashicorp/vault/vault/cluster" - "github.com/hashicorp/vault/vault/seal" -) - -//go:generate go run github.com/hashicorp/vault/tools/stubmaker -func myfunc(cluster.Listener) *seal.Envelope { return nil } -func myfunc2() *seal.Envelope { return nil } From 4b7e3c31abf875752e14c231e793ab315d37a089 Mon Sep 17 00:00:00 2001 From: Nick Cabatoff Date: Wed, 5 Jul 2023 15:24:40 -0400 Subject: [PATCH 07/10] go mod tidy --- go.sum | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/go.sum b/go.sum index 27b40d16d1c6..213099b1a6c6 100644 --- a/go.sum +++ b/go.sum @@ -684,8 +684,8 @@ github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEV github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/OneOfOne/xxhash v1.2.8/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q= -github.com/ProtonMail/go-crypto v0.0.0-20230518184743-7afd39499903 h1:ZK3C5DtzV2nVAQTx5S5jQvMeDqWtD1By5mOoyY/xJek= -github.com/ProtonMail/go-crypto v0.0.0-20230518184743-7afd39499903/go.mod h1:8TI4H3IbrackdNgv+92dI+rhpCaLqM0IfpgCgenFvRE= +github.com/ProtonMail/go-crypto v0.0.0-20230626094100-7e9e0395ebec h1:vV3RryLxt42+ZIVOFbYJCH1jsZNTNmj2NYru5zfx+4E= +github.com/ProtonMail/go-crypto v0.0.0-20230626094100-7e9e0395ebec/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= @@ -857,7 +857,7 @@ github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx2 github.com/bugsnag/bugsnag-go v0.0.0-20141110184014-b1d153021fcd/go.mod h1:2oa8nejYd4cQ/b0hMIopN0lCRxU0bueqREvZLWFrtK8= github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b/go.mod h1:obH5gd0BsqsP2LwDJ9aOkm/6J86V6lyAXCoQWGw3K50= github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE= -github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= +github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= github.com/bytecodealliance/wasmtime-go v0.36.0/go.mod h1:q320gUxqyI8yB+ZqRuaJOEnGkAnHh6WtJjMaT2CW4wI= github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= @@ -903,7 +903,6 @@ github.com/circonus-labs/circonusllhist v0.1.3 h1:TJH+oke8D16535+jHExHj4nQvzlZrj github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I= github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= -github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtMxxK7fi4I= github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs= github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= github.com/cloudfoundry-community/go-cfclient v0.0.0-20210823134051-721f0e559306 h1:k8q2Nsz7kNaUlysVCnWIFLMUSqiKXaGLdIf9P0GsX2Y= @@ -2988,6 +2987,7 @@ golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0 golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw= golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= +golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58= golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g= From 9a6439d5c17b2eb628e1b20c46e9a9a060d28445 Mon Sep 17 00:00:00 2001 From: Nick Cabatoff Date: Thu, 6 Jul 2023 09:20:58 -0400 Subject: [PATCH 08/10] Add protections to ensure we don't clobber any files that are tracked by Git in any way --- tools/stubmaker/main.go | 147 +++++++++++++++++++++++++++++++++------- 1 file changed, 122 insertions(+), 25 deletions(-) diff --git a/tools/stubmaker/main.go b/tools/stubmaker/main.go index 5bcd61de1d5d..f136c79538df 100644 --- a/tools/stubmaker/main.go +++ b/tools/stubmaker/main.go @@ -7,26 +7,109 @@ import ( "fmt" "go/types" "io" - "log" "os" + "path/filepath" "strings" "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/plumbing/object" + "github.com/hashicorp/go-hclog" "golang.org/x/tools/go/packages" ) -func isEnterprise() bool { +var logger hclog.Logger + +func fatal(err error) { + logger.Error("fatal error", "error", err) + os.Exit(1) +} + +func main() { + logger = hclog.New(&hclog.LoggerOptions{ + Name: "stubmaker", + Level: hclog.Trace, + }) + repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{ DetectDotGit: true, }) if err != nil { - log.Fatal(err) + fatal(err) } + wt, err := repo.Worktree() if err != nil { - log.Fatal(err) + fatal(err) + } + if !isEnterprise(wt) { + return + } + + head, err := repo.Head() + if err != nil { + fatal(err) + } + obj, err := repo.Object(plumbing.AnyObject, head.Hash()) + if err != nil { + fatal(err) } + st, err := wt.Status() + if err != nil { + fatal(err) + } + + inputFile := os.Getenv("GOFILE") + if !strings.HasSuffix(inputFile, "_oss.go") { + fatal(fmt.Errorf("stubmaker should only be invoked from files ending in _oss.go")) + } + + baseFilename := strings.TrimSuffix(inputFile, "_oss.go") + target := baseFilename + "_ent.go" + + tracked, err := inGit(wt, st, obj, target) + if err != nil { + fatal(err) + } + if tracked { + fatal(fmt.Errorf("output file %s exists in git, not overwriting", target)) + } + + if err := writeStubIfNeeded(inputFile, target); err != nil { + fatal(err) + } +} + +func inGit(wt *git.Worktree, st git.Status, obj object.Object, path string) (bool, error) { + absPath, err := filepath.Abs(path) + if err != nil { + return false, fmt.Errorf("path %s can't be made absolute: %w", path, err) + } + relPath, err := filepath.Rel(wt.Filesystem.Root(), absPath) + if err != nil { + return false, fmt.Errorf("path %s can't be made relative: %w", absPath, err) + } + + fst := st.File(relPath) + if fst.Worktree != git.Untracked || fst.Staging != git.Untracked { + return true, nil + } + + curwd, err := os.Getwd() + if err != nil { + return false, err + } + + blob, err := resolve(obj, relPath) + if err != nil && !strings.Contains(err.Error(), "file not found") { + return false, fmt.Errorf("error resolving path %s from %s: %w", relPath, curwd, err) + } + + return blob != nil, nil +} + +func isEnterprise(wt *git.Worktree) bool { st, err := wt.Filesystem.Stat("enthelpers") onOss := errors.Is(err, os.ErrNotExist) onEnt := st != nil @@ -35,18 +118,36 @@ func isEnterprise() bool { case onOss && !onEnt: case !onOss && onEnt: default: - log.Fatal(err) + fatal(err) } return onEnt } -func main() { - if !isEnterprise() { - return - } - baseFilename := strings.TrimSuffix(os.Getenv("GOFILE"), "_oss.go") - if err := writeStubIfNeeded(baseFilename); err != nil { - log.Fatal(err) +// resolve blob at given path from obj. obj can be a commit, tag, tree, or blob. +func resolve(obj object.Object, path string) (*object.Blob, error) { + switch o := obj.(type) { + case *object.Commit: + t, err := o.Tree() + if err != nil { + return nil, err + } + return resolve(t, path) + case *object.Tag: + target, err := o.Object() + if err != nil { + return nil, err + } + return resolve(target, path) + case *object.Tree: + file, err := o.File(path) + if err != nil { + return nil, err + } + return &file.Blob, nil + case *object.Blob: + return o, nil + default: + return nil, object.ErrUnsupportedObject } } @@ -64,11 +165,10 @@ func readLines(r io.Reader) ([]string, error) { return lines, nil } -func writeStubIfNeeded(baseFilename string) (err error) { +func writeStubIfNeeded(inputFile, outputFile string) (err error) { warning := "// Code generated by tools/stubmaker; DO NOT EDIT." var output *os.File - inputFile := fmt.Sprintf("%s_oss.go", baseFilename) b, err := os.ReadFile(inputFile) if err != nil { return err @@ -96,12 +196,9 @@ func writeStubIfNeeded(baseFilename string) (err error) { } } - target := fmt.Sprintf("%s_ent.go", baseFilename) - os.Remove(target) - pkg, err := parsePackage(".", []string{"enterprise"}) if err != nil { - log.Fatal(err) + return err } var found []string @@ -121,25 +218,25 @@ func writeStubIfNeeded(baseFilename string) (err error) { } switch { case len(found) == len(funcs): - return + return nil case len(found) != 0: - log.Fatalf("funcs partially defined: need=%v, found=%v", funcs, found) + return fmt.Errorf("funcs partially defined: need=%v, found=%v", funcs, found) } - output, err = os.Create(target + ".tmp") + output, err = os.Create(outputFile + ".tmp") if err != nil { return err } // If we don't end up writing to the file, delete it. defer func() { if err != nil { - os.Remove(target + ".tmp") + os.Remove(outputFile + ".tmp") } else { - os.Rename(target+".tmp", target) + os.Rename(outputFile+".tmp", outputFile) } }() - _, err = io.WriteString(output, strings.Join(outputLines, "\n")) + _, err = io.WriteString(output, strings.Join(outputLines, "\n")+"\n") return err } @@ -151,7 +248,7 @@ func parsePackage(name string, tags []string) (*packages.Package, error) { } pkgs, err := packages.Load(cfg, name) if err != nil { - return nil, err + return nil, fmt.Errorf("error parsing package %s: %v", name, err) } if len(pkgs) != 1 { return nil, fmt.Errorf("error: %d packages found", len(pkgs)) From cce460b2332be37eb65ae3c1d39623585ffadb15 Mon Sep 17 00:00:00 2001 From: Nick Cabatoff Date: Thu, 6 Jul 2023 09:35:09 -0400 Subject: [PATCH 09/10] Add a `go generate ./...` to `make ci-build` --- scripts/ci-helper.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/ci-helper.sh b/scripts/ci-helper.sh index 4e33a8e8aa77..c0f5e5c6504b 100755 --- a/scripts/ci-helper.sh +++ b/scripts/ci-helper.sh @@ -156,6 +156,8 @@ function build() { : "${GO_TAGS:=""}" : "${REMOVE_SYMBOLS:=""}" + go generate ./... + # Build our ldflags msg="--> Building Vault v$version, revision $revision, built $build_date" From 9ed68c3746e77f9e91b8a5245d1dfb153815ae11 Mon Sep 17 00:00:00 2001 From: Nick Cabatoff Date: Thu, 6 Jul 2023 11:54:32 -0400 Subject: [PATCH 10/10] Use local GOOS/GOARCH for go run invoked via go generate. --- scripts/ci-helper.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ci-helper.sh b/scripts/ci-helper.sh index c0f5e5c6504b..61c93cfb76b4 100755 --- a/scripts/ci-helper.sh +++ b/scripts/ci-helper.sh @@ -156,7 +156,7 @@ function build() { : "${GO_TAGS:=""}" : "${REMOVE_SYMBOLS:=""}" - go generate ./... + GOOS= GOARCH= go generate ./... # Build our ldflags msg="--> Building Vault v$version, revision $revision, built $build_date"