Skip to content

Commit b51ed86

Browse files
authored
feat: make organization ID generally available [HEAD-855] (#4887)
* feat: make organization ID generally available in golang and TS code * chore: use env var to support internal orgid * chore: add orgid to config structure
1 parent dde78c9 commit b51ed86

File tree

8 files changed

+89
-25
lines changed

8 files changed

+89
-25
lines changed

cliv2/internal/cliv2/cliv2.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"strings"
1616

1717
"github.com/gofrs/flock"
18+
"github.com/snyk/go-application-framework/pkg/configuration"
1819
"github.com/snyk/go-application-framework/pkg/utils"
1920

2021
"github.com/snyk/cli/cliv2/internal/constants"
@@ -35,6 +36,7 @@ type CLI struct {
3536
stdout io.Writer
3637
stderr io.Writer
3738
env []string
39+
globalConfig configuration.Configuration
3840
}
3941

4042
type EnvironmentWarning struct {
@@ -47,7 +49,10 @@ const (
4749
V2_ABOUT Handler = iota
4850
)
4951

50-
func NewCLIv2(cacheDirectory string, debugLogger *log.Logger) (*CLI, error) {
52+
func NewCLIv2(config configuration.Configuration, debugLogger *log.Logger) (*CLI, error) {
53+
54+
cacheDirectory := config.GetString(configuration.CACHE_PATH)
55+
5156
v1BinaryLocation, err := cliv1.GetFullCLIV1TargetPath(cacheDirectory)
5257
if err != nil {
5358
fmt.Println(err)
@@ -63,6 +68,7 @@ func NewCLIv2(cacheDirectory string, debugLogger *log.Logger) (*CLI, error) {
6368
stdout: os.Stdout,
6469
stderr: os.Stderr,
6570
env: os.Environ(),
71+
globalConfig: config,
6672
}
6773

6874
return &cli, nil
@@ -258,6 +264,7 @@ func PrepareV1EnvironmentVariables(
258264
integrationVersion string,
259265
proxyAddress string,
260266
caCertificateLocation string,
267+
orgid string,
261268
) (result []string, err error) {
262269

263270
inputAsMap := utils.ToKeyValueMap(input, "=")
@@ -302,6 +309,7 @@ func PrepareV1EnvironmentVariables(
302309
inputAsMap[constants.SNYK_HTTPS_PROXY_ENV] = proxyAddress
303310
inputAsMap[constants.SNYK_HTTP_PROXY_ENV] = proxyAddress
304311
inputAsMap[constants.SNYK_CA_CERTIFICATE_LOCATION_ENV] = caCertificateLocation
312+
inputAsMap[constants.SNYK_INTERNAL_ORGID_ENV] = orgid
305313

306314
// merge user defined (external) and internal no_proxy configuration
307315
if len(inputAsMap[constants.SNYK_HTTP_NO_PROXY_ENV_SYSTEM]) > 0 {
@@ -328,9 +336,10 @@ func (c *CLI) PrepareV1Command(
328336
integrationVersion string,
329337
) (snykCmd *exec.Cmd, err error) {
330338
proxyAddress := fmt.Sprintf("http://%s:%[email protected]:%d", proxy.PROXY_USERNAME, proxyInfo.Password, proxyInfo.Port)
339+
orgid := c.globalConfig.GetString(configuration.ORGANIZATION)
331340

332341
snykCmd = exec.Command(cmd, args...)
333-
snykCmd.Env, err = PrepareV1EnvironmentVariables(c.env, integrationName, integrationVersion, proxyAddress, proxyInfo.CertificateLocation)
342+
snykCmd.Env, err = PrepareV1EnvironmentVariables(c.env, integrationName, integrationVersion, proxyAddress, proxyInfo.CertificateLocation, orgid)
334343

335344
if len(c.WorkingDirectory) > 0 {
336345
snykCmd.Dir = c.WorkingDirectory
@@ -340,6 +349,7 @@ func (c *CLI) PrepareV1Command(
340349
}
341350

342351
func (c *CLI) executeV1Default(proxyInfo *proxy.ProxyInfo, passThroughArgs []string) error {
352+
343353
snykCmd, err := c.PrepareV1Command(c.v1BinaryLocation, passThroughArgs, proxyInfo, c.GetIntegrationName(), GetFullVersion())
344354

345355
if c.DebugLogger.Writer() != io.Discard {

cliv2/internal/cliv2/cliv2_test.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"testing"
1111
"time"
1212

13+
"github.com/snyk/go-application-framework/pkg/configuration"
14+
1315
"github.com/snyk/cli/cliv2/internal/cliv2"
1416
"github.com/snyk/cli/cliv2/internal/constants"
1517
"github.com/snyk/cli/cliv2/internal/proxy"
@@ -54,10 +56,11 @@ func Test_PrepareV1EnvironmentVariables_Fill_and_Filter(t *testing.T) {
5456
"SNYK_SYSTEM_NO_PROXY=noProxy",
5557
"SNYK_SYSTEM_HTTP_PROXY=httpProxy",
5658
"SNYK_SYSTEM_HTTPS_PROXY=httpsProxy",
59+
"SNYK_INTERNAL_ORGID=orgid",
5760
"NO_PROXY=" + constants.SNYK_INTERNAL_NO_PROXY + ",noProxy",
5861
}
5962

60-
actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation")
63+
actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation", "orgid")
6164

6265
sort.Strings(expected)
6366
sort.Strings(actual)
@@ -80,10 +83,11 @@ func Test_PrepareV1EnvironmentVariables_DontOverrideExistingIntegration(t *testi
8083
"SNYK_SYSTEM_NO_PROXY=",
8184
"SNYK_SYSTEM_HTTP_PROXY=",
8285
"SNYK_SYSTEM_HTTPS_PROXY=",
86+
"SNYK_INTERNAL_ORGID=orgid",
8387
"NO_PROXY=" + constants.SNYK_INTERNAL_NO_PROXY,
8488
}
8589

86-
actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation")
90+
actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation", "orgid")
8791

8892
sort.Strings(expected)
8993
sort.Strings(actual)
@@ -106,10 +110,11 @@ func Test_PrepareV1EnvironmentVariables_OverrideProxyAndCerts(t *testing.T) {
106110
"SNYK_SYSTEM_NO_PROXY=312123",
107111
"SNYK_SYSTEM_HTTP_PROXY=exists",
108112
"SNYK_SYSTEM_HTTPS_PROXY=already",
113+
"SNYK_INTERNAL_ORGID=orgid",
109114
"NO_PROXY=" + constants.SNYK_INTERNAL_NO_PROXY + ",312123",
110115
}
111116

112-
actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation")
117+
actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation", "orgid")
113118

114119
sort.Strings(expected)
115120
sort.Strings(actual)
@@ -122,7 +127,7 @@ func Test_PrepareV1EnvironmentVariables_Fail_DontOverrideExisting(t *testing.T)
122127
input := []string{"something=1", "in=2", "here=3", "SNYK_INTEGRATION_NAME=exists"}
123128
expected := input
124129

125-
actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "unused", "unused")
130+
actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "unused", "unused", "orgid")
126131

127132
sort.Strings(expected)
128133
sort.Strings(actual)
@@ -144,7 +149,9 @@ func getProxyInfoForTest() *proxy.ProxyInfo {
144149
func Test_prepareV1Command(t *testing.T) {
145150
expectedArgs := []string{"hello", "world"}
146151
cacheDir := getCacheDir(t)
147-
cli, _ := cliv2.NewCLIv2(cacheDir, discardLogger)
152+
config := configuration.NewInMemory()
153+
config.Set(configuration.CACHE_PATH, cacheDir)
154+
cli, _ := cliv2.NewCLIv2(config, discardLogger)
148155

149156
snykCmd, err := cli.PrepareV1Command(
150157
"someExecutable",
@@ -165,11 +172,13 @@ func Test_prepareV1Command(t *testing.T) {
165172
func Test_extractOnlyOnce(t *testing.T) {
166173
cacheDir := getCacheDir(t)
167174
tmpDir := utils.GetTemporaryDirectory(cacheDir, cliv2.GetFullVersion())
175+
config := configuration.NewInMemory()
176+
config.Set(configuration.CACHE_PATH, cacheDir)
168177

169178
assert.NoDirExists(t, tmpDir)
170179

171180
// create instance under test
172-
cli, _ := cliv2.NewCLIv2(cacheDir, discardLogger)
181+
cli, _ := cliv2.NewCLIv2(config, discardLogger)
173182

174183
// run once
175184
assert.Nil(t, cli.Init())
@@ -192,11 +201,13 @@ func Test_extractOnlyOnce(t *testing.T) {
192201
func Test_init_extractDueToInvalidBinary(t *testing.T) {
193202
cacheDir := getCacheDir(t)
194203
tmpDir := utils.GetTemporaryDirectory(cacheDir, cliv2.GetFullVersion())
204+
config := configuration.NewInMemory()
205+
config.Set(configuration.CACHE_PATH, cacheDir)
195206

196207
assert.NoDirExists(t, tmpDir)
197208

198209
// create instance under test
199-
cli, _ := cliv2.NewCLIv2(cacheDir, discardLogger)
210+
cli, _ := cliv2.NewCLIv2(config, discardLogger)
200211

201212
// fill binary with invalid data
202213
_ = os.MkdirAll(tmpDir, 0755)
@@ -227,11 +238,13 @@ func Test_executeRunV2only(t *testing.T) {
227238

228239
cacheDir := getCacheDir(t)
229240
tmpDir := utils.GetTemporaryDirectory(cacheDir, cliv2.GetFullVersion())
241+
config := configuration.NewInMemory()
242+
config.Set(configuration.CACHE_PATH, cacheDir)
230243

231244
assert.NoDirExists(t, tmpDir)
232245

233246
// create instance under test
234-
cli, _ := cliv2.NewCLIv2(cacheDir, discardLogger)
247+
cli, _ := cliv2.NewCLIv2(config, discardLogger)
235248
assert.Nil(t, cli.Init())
236249

237250
actualReturnCode := cliv2.DeriveExitCode(cli.Execute(getProxyInfoForTest(), []string{"--version"}))
@@ -244,9 +257,11 @@ func Test_executeUnknownCommand(t *testing.T) {
244257
expectedReturnCode := constants.SNYK_EXIT_CODE_ERROR
245258

246259
cacheDir := getCacheDir(t)
260+
config := configuration.NewInMemory()
261+
config.Set(configuration.CACHE_PATH, cacheDir)
247262

248263
// create instance under test
249-
cli, _ := cliv2.NewCLIv2(cacheDir, discardLogger)
264+
cli, _ := cliv2.NewCLIv2(config, discardLogger)
250265
assert.Nil(t, cli.Init())
251266

252267
actualReturnCode := cliv2.DeriveExitCode(cli.Execute(getProxyInfoForTest(), []string{"bogusCommand"}))
@@ -255,9 +270,11 @@ func Test_executeUnknownCommand(t *testing.T) {
255270

256271
func Test_clearCache(t *testing.T) {
257272
cacheDir := getCacheDir(t)
273+
config := configuration.NewInMemory()
274+
config.Set(configuration.CACHE_PATH, cacheDir)
258275

259276
// create instance under test
260-
cli, _ := cliv2.NewCLIv2(cacheDir, discardLogger)
277+
cli, _ := cliv2.NewCLIv2(config, discardLogger)
261278
assert.Nil(t, cli.Init())
262279

263280
// create folders and files in cache dir
@@ -287,9 +304,11 @@ func Test_clearCache(t *testing.T) {
287304

288305
func Test_clearCacheBigCache(t *testing.T) {
289306
cacheDir := getCacheDir(t)
307+
config := configuration.NewInMemory()
308+
config.Set(configuration.CACHE_PATH, cacheDir)
290309

291310
// create instance under test
292-
cli, _ := cliv2.NewCLIv2(cacheDir, discardLogger)
311+
cli, _ := cliv2.NewCLIv2(config, discardLogger)
293312
assert.Nil(t, cli.Init())
294313

295314
// create folders and files in cache dir

cliv2/internal/constants/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const SNYK_CA_CERTIFICATE_LOCATION_ENV = "NODE_EXTRA_CA_CERTS"
1717
const SNYK_INTERNAL_NO_PROXY = "localhost,127.0.0.1,::1"
1818
const SNYK_OAUTH_ACCESS_TOKEN_ENV = "SNYK_OAUTH_TOKEN"
1919
const SNYK_ANALYTICS_DISABLED_ENV = "SNYK_DISABLE_ANALYTICS"
20+
const SNYK_INTERNAL_ORGID_ENV = "SNYK_INTERNAL_ORGID"
2021
const SNYK_OPENSSL_CONF = "OPENSSL_CONF"
2122

2223
const SNYK_HTTPS_PROXY_ENV_SYSTEM = "SNYK_SYSTEM_HTTPS_PROXY"

cliv2/internal/proxy/proxy.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313

1414
"github.com/google/uuid"
15+
"github.com/snyk/go-application-framework/pkg/configuration"
1516

1617
"github.com/snyk/go-application-framework/pkg/networking/certs"
1718
"github.com/snyk/go-httpauth/pkg/httpauth"
@@ -49,12 +50,15 @@ const (
4950
PROXY_USERNAME = "snykcli"
5051
)
5152

52-
func NewWrapperProxy(insecureSkipVerify bool, cacheDirectory string, cliVersion string, debugLogger *log.Logger) (*WrapperProxy, error) {
53+
func NewWrapperProxy(config configuration.Configuration, cliVersion string, debugLogger *log.Logger) (*WrapperProxy, error) {
5354
var p WrapperProxy
5455
p.DebugLogger = debugLogger
5556
p.cliVersion = cliVersion
5657
p.addHeaderFunc = func(request *http.Request) error { return nil }
5758

59+
cacheDirectory := config.GetString(configuration.CACHE_PATH)
60+
insecureSkipVerify := config.GetBool(configuration.INSECURE_HTTPS)
61+
5862
certName := "snyk-embedded-proxy"
5963
certPEMBlock, keyPEMBlock, err := certs.MakeSelfSignedCert(certName, []string{}, p.DebugLogger)
6064
if err != nil {

cliv2/internal/proxy/proxy_test.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import (
1212
"os"
1313
"testing"
1414

15+
"github.com/snyk/go-application-framework/pkg/configuration"
16+
"github.com/snyk/go-application-framework/pkg/networking/certs"
17+
"github.com/snyk/go-httpauth/pkg/httpauth"
18+
1519
"github.com/snyk/cli/cliv2/internal/constants"
1620
"github.com/snyk/cli/cliv2/internal/proxy"
1721
"github.com/snyk/cli/cliv2/internal/utils"
18-
"github.com/snyk/go-application-framework/pkg/networking/certs"
19-
"github.com/snyk/go-httpauth/pkg/httpauth"
2022

2123
"github.com/stretchr/testify/assert"
2224
)
@@ -80,7 +82,11 @@ func Test_closingProxyDeletesTempCert(t *testing.T) {
8082
setup(t, basecache, version)
8183
defer teardown(t, basecache)
8284

83-
wp, err := proxy.NewWrapperProxy(false, basecache, version, debugLogger)
85+
config := configuration.NewInMemory()
86+
config.Set(configuration.CACHE_PATH, basecache)
87+
config.Set(configuration.INSECURE_HTTPS, false)
88+
89+
wp, err := proxy.NewWrapperProxy(config, version, debugLogger)
8490
assert.Nil(t, err)
8591

8692
err = wp.Start()
@@ -101,10 +107,14 @@ func basicAuthValue(username string, password string) string {
101107
func Test_canGoThroughProxy(t *testing.T) {
102108
basecache := "testcache"
103109
version := "1.1.1"
110+
config := configuration.NewInMemory()
111+
config.Set(configuration.CACHE_PATH, basecache)
112+
config.Set(configuration.INSECURE_HTTPS, false)
113+
104114
setup(t, basecache, version)
105115
defer teardown(t, basecache)
106116

107-
wp, err := proxy.NewWrapperProxy(false, basecache, version, debugLogger)
117+
wp, err := proxy.NewWrapperProxy(config, version, debugLogger)
108118
assert.Nil(t, err)
109119

110120
err = wp.Start()
@@ -130,10 +140,14 @@ func Test_canGoThroughProxy(t *testing.T) {
130140
func Test_proxyRejectsWithoutBasicAuthHeader(t *testing.T) {
131141
basecache := "testcache"
132142
version := "1.1.1"
143+
config := configuration.NewInMemory()
144+
config.Set(configuration.CACHE_PATH, basecache)
145+
config.Set(configuration.INSECURE_HTTPS, false)
146+
133147
setup(t, basecache, version)
134148
defer teardown(t, basecache)
135149

136-
wp, err := proxy.NewWrapperProxy(false, basecache, version, debugLogger)
150+
wp, err := proxy.NewWrapperProxy(config, version, debugLogger)
137151
assert.Nil(t, err)
138152

139153
err = wp.Start()
@@ -158,6 +172,10 @@ func Test_proxyRejectsWithoutBasicAuthHeader(t *testing.T) {
158172
func Test_SetUpstreamProxy(t *testing.T) {
159173
basecache := "testcache"
160174
version := "1.1.1"
175+
config := configuration.NewInMemory()
176+
config.Set(configuration.CACHE_PATH, basecache)
177+
config.Set(configuration.INSECURE_HTTPS, false)
178+
161179
setup(t, basecache, version)
162180
defer teardown(t, basecache)
163181

@@ -178,7 +196,7 @@ func Test_SetUpstreamProxy(t *testing.T) {
178196
httpauth.UnknownMechanism,
179197
}
180198

181-
objectUnderTest, err = proxy.NewWrapperProxy(false, basecache, version, debugLogger)
199+
objectUnderTest, err = proxy.NewWrapperProxy(config, version, debugLogger)
182200
assert.Nil(t, err)
183201

184202
// running different cases
@@ -210,6 +228,10 @@ func Test_SetUpstreamProxy(t *testing.T) {
210228
func Test_appendExtraCaCert(t *testing.T) {
211229
basecache := "testcache"
212230
version := "1.1.1"
231+
config := configuration.NewInMemory()
232+
config.Set(configuration.CACHE_PATH, basecache)
233+
config.Set(configuration.INSECURE_HTTPS, false)
234+
213235
setup(t, basecache, version)
214236
defer teardown(t, basecache)
215237

@@ -219,7 +241,7 @@ func Test_appendExtraCaCert(t *testing.T) {
219241

220242
os.Setenv(constants.SNYK_CA_CERTIFICATE_LOCATION_ENV, file.Name())
221243

222-
wp, err := proxy.NewWrapperProxy(false, basecache, version, debugLogger)
244+
wp, err := proxy.NewWrapperProxy(config, version, debugLogger)
223245
assert.Nil(t, err)
224246

225247
certsPem, err := os.ReadFile(wp.CertificateLocation)

cliv2/pkg/basic_workflows/legacycli.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ func legacycliWorkflow(
7272
args := config.GetStringSlice(configuration.RAW_CMD_ARGS)
7373
useStdIo := config.GetBool(configuration.WORKFLOW_USE_STDIO)
7474
isDebug := config.GetBool(configuration.DEBUG)
75-
cacheDirectory := config.GetString(configuration.CACHE_PATH)
7675
workingDirectory := config.GetString(configuration.WORKING_DIRECTORY)
77-
insecure := config.GetBool(configuration.INSECURE_HTTPS)
7876
proxyAuthenticationMechanismString := config.GetString(configuration.PROXY_AUTHENTICATION_MECHANISM)
7977
proxyAuthenticationMechanism := httpauth.AuthenticationMechanismFromString(proxyAuthenticationMechanismString)
8078
analyticsDisabled := config.GetBool(configuration.ANALYTICS_DISABLED)
@@ -85,7 +83,7 @@ func legacycliWorkflow(
8583

8684
// init cli object
8785
var cli *cliv2.CLI
88-
cli, err = cliv2.NewCLIv2(cacheDirectory, debugLogger)
86+
cli, err = cliv2.NewCLIv2(config, debugLogger)
8987
if err != nil {
9088
return output, err
9189
}
@@ -129,7 +127,7 @@ func legacycliWorkflow(
129127
}
130128

131129
// init proxy object
132-
wrapperProxy, err := proxy.NewWrapperProxy(insecure, cacheDirectory, cliv2.GetFullVersion(), debugLogger)
130+
wrapperProxy, err := proxy.NewWrapperProxy(config, cliv2.GetFullVersion(), debugLogger)
133131
if err != nil {
134132
return output, errors.Wrap(err, "Failed to create proxy!")
135133
}

0 commit comments

Comments
 (0)