Skip to content

Commit

Permalink
sync upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
yandex-cloud-bot committed Jun 21, 2024
1 parent 8b0dc5b commit 1018f7c
Show file tree
Hide file tree
Showing 15 changed files with 657 additions and 3 deletions.
13 changes: 12 additions & 1 deletion credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net"
"net/http"
"net/http/httputil"
"os"
"time"

"github.com/golang-jwt/jwt/v4"
Expand Down Expand Up @@ -91,9 +92,19 @@ func ServiceAccountKey(key *iamkey.Key) (Credentials, error) {
// That is, for SDK build with InstanceServiceAccount credentials and used on Compute Instance
// created with yandex.cloud.compute.v1.CreateInstanceRequest.service_account_id, API calls
// will be authenticated with this ServiceAccount ID.
// You can override the default address of Metadata Service by setting env variable.
// TODO(skipor): doc link
func InstanceServiceAccount() NonExchangeableCredentials {
return newInstanceServiceAccountCredentials(InstanceMetadataAddr)
return newInstanceServiceAccountCredentials(getMetadataServiceAddr())
}

// getMetadataServiceAddr returns the address of Metadata Service, gets the value from InstanceMetadataOverrideEnvVar
// env variable if it is set, otherwise uses the default address from InstanceMetadataAddr.
func getMetadataServiceAddr() string {
if nonDefaultAddr := os.Getenv(InstanceMetadataOverrideEnvVar); nonDefaultAddr != "" {
return nonDefaultAddr
}
return InstanceMetadataAddr
}

func newServiceAccountJWTBuilder(key *iamkey.Key) (*serviceAccountJWTBuilder, error) {
Expand Down
22 changes: 22 additions & 0 deletions credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -117,6 +118,27 @@ func TestInstanceServiceAccount(t *testing.T) {
})
}

func TestMetadataServiceOverride(t *testing.T) {
t.Run("should return the default value", func(t *testing.T) {
// GIVEN
expected := InstanceMetadataAddr
// WHEN
actual := getMetadataServiceAddr()
// THEN
require.Equal(t, expected, actual)
})
t.Run("should be equal to value from env variable", func(t *testing.T) {
// GIVEN
expected := "69.69.69.69"
require.NoError(t, os.Setenv(InstanceMetadataOverrideEnvVar, expected))
// WHEN
actual := getMetadataServiceAddr()
// THEN
require.Equal(t, expected, actual)
require.NoError(t, os.Unsetenv(InstanceMetadataOverrideEnvVar))
})
}

func testKey(t *testing.T) *iamkey.Key {
key, err := iamkey.ReadFromJSONFile("test_data/service_account_key.json")
require.NoError(t, err)
Expand Down
115 changes: 115 additions & 0 deletions gen/iam/apikey.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions gen/iot/devices/registry.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

175 changes: 175 additions & 0 deletions gen/smartcaptcha/captcha.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1018f7c

Please sign in to comment.