-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
App Store Connect API client improvements (#256)
* Use jwt.RegisteredClaims instead of custom claims and set IssuedAt claim * Add app store connect api client integration test * Update integration tests * Use check step from upgrade-golangci-lint branch * Fix lint issues * Use check step from master branch * Log warning for too many requests response * Update comment * Add rate limit info to warning messages
- Loading branch information
Showing
12 changed files
with
486 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ _tmp/ | |
.vscode/* | ||
.idea/* | ||
**/.idea/* | ||
.DS_Store | ||
.DS_Store | ||
.env |
69 changes: 69 additions & 0 deletions
69
_integration_tests/appstoreconnect_tests/api_key_credential_helper.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package appstoreconnect_tests | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/bitrise-io/go-utils/v2/log" | ||
"github.com/bitrise-io/go-utils/v2/retryhttp" | ||
"github.com/bitrise-io/go-xcode/v2/_integration_tests" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func getAPIKey(t *testing.T) (string, string, []byte, bool) { | ||
if os.Getenv("TEST_API_KEY") != "" { | ||
return getLocalAPIKey(t) | ||
} | ||
return getRemoteAPIKey(t) | ||
} | ||
|
||
func getLocalAPIKey(t *testing.T) (string, string, []byte, bool) { | ||
keyID := os.Getenv("TEST_API_KEY_ID") | ||
require.NotEmpty(t, keyID) | ||
issuerID := os.Getenv("TEST_API_KEY_ISSUER_ID") | ||
require.NotEmpty(t, issuerID) | ||
privateKey := os.Getenv("TEST_API_KEY") | ||
require.NotEmpty(t, privateKey) | ||
isEnterpriseAPIKey := os.Getenv("TEST_API_KEY_IS_ENTERPRISE") == "true" | ||
|
||
return keyID, issuerID, []byte(privateKey), isEnterpriseAPIKey | ||
} | ||
|
||
func getRemoteAPIKey(t *testing.T) (string, string, []byte, bool) { | ||
serviceAccountJSON := os.Getenv("GCS_SERVICE_ACCOUNT_JSON") | ||
require.NotEmpty(t, serviceAccountJSON) | ||
projectID := os.Getenv("GCS_PROJECT_ID") | ||
require.NotEmpty(t, projectID) | ||
bucketName := os.Getenv("GCS_BUCKET_NAME") | ||
require.NotEmpty(t, bucketName) | ||
|
||
secretAccessor, err := _integration_tests.NewSecretAccessor(serviceAccountJSON, projectID) | ||
require.NoError(t, err) | ||
|
||
bucketAccessor, err := _integration_tests.NewBucketAccessor(serviceAccountJSON, bucketName) | ||
require.NoError(t, err) | ||
|
||
keyID, err := secretAccessor.GetSecret("BITRISE_APPSTORECONNECT_API_KEY_ID") | ||
require.NoError(t, err) | ||
|
||
issuerID, err := secretAccessor.GetSecret("BITRISE_APPSTORECONNECT_API_KEY_ISSUER_ID") | ||
require.NoError(t, err) | ||
|
||
keyURL, err := secretAccessor.GetSecret("BITRISE_APPSTORECONNECT_API_KEY_URL") | ||
require.NoError(t, err) | ||
|
||
keyDownloadURL, err := bucketAccessor.GetExpiringURL(keyURL) | ||
require.NoError(t, err) | ||
|
||
logger := log.NewLogger() | ||
logger.EnableDebugLog(false) | ||
client := retryhttp.NewClient(logger) | ||
resp, err := client.Get(keyDownloadURL) | ||
require.NoError(t, err) | ||
|
||
privateKey, err := io.ReadAll(resp.Body) | ||
require.NoError(t, err) | ||
|
||
return keyID, issuerID, privateKey, false | ||
} |
18 changes: 18 additions & 0 deletions
18
_integration_tests/appstoreconnect_tests/appstoreconnect_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package appstoreconnect_tests | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/bitrise-io/go-xcode/v2/autocodesign/devportalclient/appstoreconnect" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestListBundleIDs(t *testing.T) { | ||
keyID, issuerID, privateKey, enterpriseAccount := getAPIKey(t) | ||
|
||
client := appstoreconnect.NewClient(appstoreconnect.NewRetryableHTTPClient(), keyID, issuerID, []byte(privateKey), enterpriseAccount) | ||
|
||
response, err := client.Provisioning.ListBundleIDs(&appstoreconnect.ListBundleIDsOptions{}) | ||
require.NoError(t, err) | ||
require.True(t, len(response.Data) > 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package _integration_tests | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"cloud.google.com/go/storage" | ||
"golang.org/x/oauth2/google" | ||
"golang.org/x/oauth2/jwt" | ||
) | ||
|
||
// BucketAccessor ... | ||
type BucketAccessor struct { | ||
jwtConfig *jwt.Config | ||
bucket string | ||
objectExpiry time.Duration | ||
} | ||
|
||
// NewBucketAccessor ... | ||
func NewBucketAccessor(serviceAccountJSONContent, bucket string) (*BucketAccessor, error) { | ||
conf, err := google.JWTConfigFromJSON([]byte(serviceAccountJSONContent)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &BucketAccessor{ | ||
jwtConfig: conf, | ||
bucket: bucket, | ||
objectExpiry: 1 * time.Hour, | ||
}, nil | ||
} | ||
|
||
// GetExpiringURL ... | ||
func (a BucketAccessor) GetExpiringURL(originalURL string) (string, error) { | ||
artifactPath := strings.TrimPrefix(strings.TrimPrefix(originalURL, fmt.Sprintf("https://storage.googleapis.com/%s/", a.bucket)), fmt.Sprintf("https://storage.cloud.google.com/%s/", a.bucket)) | ||
opts := &storage.SignedURLOptions{ | ||
Method: http.MethodGet, | ||
GoogleAccessID: a.jwtConfig.Email, | ||
PrivateKey: a.jwtConfig.PrivateKey, | ||
Expires: time.Now().Add(a.objectExpiry), | ||
} | ||
|
||
return storage.SignedURL(a.bucket, artifactPath, opts) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package _integration_tests | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
secretmanager "cloud.google.com/go/secretmanager/apiv1" | ||
secretmanagerpb "cloud.google.com/go/secretmanager/apiv1/secretmanagerpb" | ||
"github.com/bitrise-io/go-utils/log" | ||
"github.com/bitrise-io/go-utils/retry" | ||
"google.golang.org/api/option" | ||
) | ||
|
||
// SecretAccessor ... | ||
type SecretAccessor struct { | ||
ctx context.Context | ||
client *secretmanager.Client | ||
projectID string | ||
} | ||
|
||
// NewSecretAccessor ... | ||
func NewSecretAccessor(serviceAccountJSONContent, projectID string) (*SecretAccessor, error) { | ||
ctx := context.Background() | ||
client, err := secretmanager.NewClient(ctx, option.WithCredentialsJSON([]byte(serviceAccountJSONContent))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &SecretAccessor{ | ||
ctx: ctx, | ||
client: client, | ||
projectID: projectID, | ||
}, nil | ||
} | ||
|
||
// GetSecret ... | ||
func (m SecretAccessor) GetSecret(key string) (string, error) { | ||
secretValue := "" | ||
if err := retry.Times(3).Wait(30 * time.Second).Try(func(attempt uint) error { | ||
if attempt > 0 { | ||
log.Warnf("%d attempt failed", attempt) | ||
} | ||
|
||
name := fmt.Sprintf("projects/%s/secrets/%s/versions/latest", m.projectID, key) | ||
req := &secretmanagerpb.AccessSecretVersionRequest{ | ||
Name: name, | ||
} | ||
result, err := m.client.AccessSecretVersion(m.ctx, req) | ||
if err != nil { | ||
log.Warnf("%s", err) | ||
return err | ||
} | ||
|
||
secretValue = string(result.Payload.Data) | ||
return nil | ||
}); err != nil { | ||
return "", err | ||
} | ||
|
||
return secretValue, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.