Skip to content

Commit c40dc36

Browse files
committed
chore(deps): reduce aws dependencies
1 parent 9d78b63 commit c40dc36

7 files changed

Lines changed: 24 additions & 98 deletions

File tree

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ go 1.24
55
require (
66
github.com/404tk/go-prompt v0.0.1
77
github.com/404tk/table v0.0.4
8-
github.com/aws/aws-sdk-go-v2 v1.41.6
98
golang.org/x/oauth2 v0.12.0
109
gopkg.in/yaml.v3 v3.0.1
1110
)
1211

1312
require (
1413
cloud.google.com/go/compute v1.20.1 // indirect
1514
cloud.google.com/go/compute/metadata v0.2.3 // indirect
16-
github.com/aws/smithy-go v1.25.0 // indirect
1715
github.com/golang/protobuf v1.5.3 // indirect
1816
github.com/kr/pretty v0.2.0 // indirect
1917
github.com/mattn/go-colorable v0.1.12 // indirect

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ github.com/404tk/go-prompt v0.0.1 h1:PUa2qXxzidYrn0sZITvMTCQ6wKqpODB06ohcjPs60RU
66
github.com/404tk/go-prompt v0.0.1/go.mod h1:BU3cEVX1xhNDco8T27cBQ8lzkY8P+xUCLotUpjTJR7o=
77
github.com/404tk/table v0.0.4 h1:WlhqtfsX8zEHMwBJRknq2D5J2ew9EvMHw4ahD8EBSq0=
88
github.com/404tk/table v0.0.4/go.mod h1:bMAHHOKSl6wXuSY6TX6t/f3/XfCNsbjmzZKb6S9PjsQ=
9-
github.com/aws/aws-sdk-go-v2 v1.41.6 h1:1AX0AthnBQzMx1vbmir3Y4WsnJgiydmnJjiLu+LvXOg=
10-
github.com/aws/aws-sdk-go-v2 v1.41.6/go.mod h1:dy0UzBIfwSeot4grGvY1AqFWN5zgziMmWGzysDnHFcQ=
11-
github.com/aws/smithy-go v1.25.0 h1:Sz/XJ64rwuiKtB6j98nDIPyYrV1nVNJ4YU74gttcl5U=
12-
github.com/aws/smithy-go v1.25.0/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc=
139
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1410
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
1511
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=

pkg/providers/aws/auth/credential.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package auth
22

33
import (
4-
"context"
54
"errors"
65
"strings"
76

87
"github.com/404tk/cloudtoolkit/pkg/schema"
98
"github.com/404tk/cloudtoolkit/utils"
10-
awsv2 "github.com/aws/aws-sdk-go-v2/aws"
119
)
1210

13-
// Credential is the provider-local AWS credential shape used by the
14-
// lightweight STS client and direct sdk.Config construction.
11+
// Credential is the provider-local AWS credential shape consumed by the
12+
// lightweight STS / EC2 / IAM / S3 API clients under pkg/providers/aws/api.
1513
type Credential struct {
1614
AccessKeyID string
1715
SecretAccessKey string
@@ -49,15 +47,3 @@ func (c Credential) Validate() error {
4947
return nil
5048
}
5149
}
52-
53-
func (c Credential) Retrieve(context.Context) (awsv2.Credentials, error) {
54-
if err := c.Validate(); err != nil {
55-
return awsv2.Credentials{}, err
56-
}
57-
return awsv2.Credentials{
58-
AccessKeyID: c.AccessKeyID,
59-
SecretAccessKey: c.SecretAccessKey,
60-
SessionToken: c.SessionToken,
61-
Source: "ctk-static",
62-
}, nil
63-
}

pkg/providers/aws/auth/credential_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package auth
22

33
import (
4-
"context"
54
"testing"
65

76
"github.com/404tk/cloudtoolkit/pkg/schema"
87
"github.com/404tk/cloudtoolkit/utils"
98
)
109

11-
func TestFromOptionsAndRetrieve(t *testing.T) {
10+
func TestFromOptionsParsesCredential(t *testing.T) {
1211
options := schema.Options{
1312
utils.AccessKey: "ak",
1413
utils.SecretKey: "sk",
@@ -19,12 +18,8 @@ func TestFromOptionsAndRetrieve(t *testing.T) {
1918
if err != nil {
2019
t.Fatalf("FromOptions() error = %v", err)
2120
}
22-
creds, err := credential.Retrieve(context.Background())
23-
if err != nil {
24-
t.Fatalf("Retrieve() error = %v", err)
25-
}
26-
if creds.AccessKeyID != "ak" || creds.SecretAccessKey != "sk" || creds.SessionToken != "token" {
27-
t.Fatalf("unexpected credentials: %+v", creds)
21+
if credential.AccessKeyID != "ak" || credential.SecretAccessKey != "sk" || credential.SessionToken != "token" {
22+
t.Fatalf("unexpected credential: %+v", credential)
2823
}
2924
}
3025

pkg/providers/aws/aws.go

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ import (
1313
"github.com/404tk/cloudtoolkit/utils"
1414
"github.com/404tk/cloudtoolkit/utils/cache"
1515
"github.com/404tk/cloudtoolkit/utils/logger"
16-
awsv2 "github.com/aws/aws-sdk-go-v2/aws"
1716
)
1817

1918
// Provider is a data provider for aws API
2019
type Provider struct {
21-
region string
22-
cfg awsv2.Config
23-
apiClient *_api.Client
20+
region string
21+
defaultRegion string
22+
apiClient *_api.Client
2423
}
2524

2625
// New creates a new provider client for aws API
@@ -29,25 +28,19 @@ func New(options schema.Options) (*Provider, error) {
2928
if err != nil {
3029
return nil, err
3130
}
32-
region, _ := options.GetMetadata(utils.Region)
33-
version, _ := options.GetMetadata(utils.Version)
34-
cfg, err := newConfig(
35-
credential.AccessKeyID,
36-
credential.SecretAccessKey,
37-
credential.SessionToken,
38-
region,
39-
version,
40-
)
41-
if err != nil {
31+
if err := credential.Validate(); err != nil {
4232
return nil, err
4333
}
34+
region, _ := options.GetMetadata(utils.Region)
35+
version, _ := options.GetMetadata(utils.Version)
36+
defaultRegion := resolveBootstrapRegion(region, version)
4437
apiClient := _api.NewClient(credential)
4538

4639
payload, _ := options.GetMetadata(utils.Payload)
4740
if payload == "cloudlist" {
4841
resp, err := apiClient.GetCallerIdentity(
4942
context.Background(),
50-
resolveBootstrapRegion(region, version),
43+
defaultRegion,
5144
)
5245
if err != nil {
5346
return nil, err
@@ -59,9 +52,9 @@ func New(options schema.Options) (*Provider, error) {
5952
}
6053

6154
return &Provider{
62-
region: region,
63-
cfg: cfg,
64-
apiClient: apiClient,
55+
region: region,
56+
defaultRegion: defaultRegion,
57+
apiClient: apiClient,
6558
}, nil
6659
}
6760

@@ -80,7 +73,7 @@ func (p *Provider) Resources(ctx context.Context) (schema.Resources, error) {
8073
ec2provider := &_ec2.Driver{
8174
Client: p.apiClient,
8275
Region: p.region,
83-
DefaultRegion: p.cfg.Region,
76+
DefaultRegion: p.defaultRegion,
8477
}
8578
hosts, err := ec2provider.GetResource(ctx)
8679
schema.AppendAssets(&list, hosts)
@@ -90,13 +83,13 @@ func (p *Provider) Resources(ctx context.Context) (schema.Resources, error) {
9083
iamprovider := &_iam.Driver{
9184
Client: p.apiClient,
9285
Region: p.region,
93-
DefaultRegion: p.cfg.Region,
86+
DefaultRegion: p.defaultRegion,
9487
}
9588
users, err := iamprovider.ListUsers(ctx)
9689
schema.AppendAssets(&list, users)
9790
list.AddError("account", err)
9891
case "bucket":
99-
s3provider := &_s3.Driver{Client: p.apiClient, DefaultRegion: p.cfg.Region}
92+
s3provider := &_s3.Driver{Client: p.apiClient, DefaultRegion: p.defaultRegion}
10093
storages, err := s3provider.GetBuckets(ctx)
10194
schema.AppendAssets(&list, storages)
10295
list.AddError("bucket", err)
@@ -111,7 +104,7 @@ func (p *Provider) UserManagement(action, username, password string) {
111104
ramprovider := &_iam.Driver{
112105
Client: p.apiClient,
113106
Region: p.region,
114-
DefaultRegion: p.cfg.Region,
107+
DefaultRegion: p.defaultRegion,
115108
Username: username,
116109
Password: password,
117110
}
@@ -126,7 +119,7 @@ func (p *Provider) UserManagement(action, username, password string) {
126119
}
127120

128121
func (p *Provider) BucketDump(ctx context.Context, action, bucketName string) {
129-
s3provider := &_s3.Driver{Client: p.apiClient, DefaultRegion: p.cfg.Region}
122+
s3provider := &_s3.Driver{Client: p.apiClient, DefaultRegion: p.defaultRegion}
130123
switch action {
131124
case "list":
132125
var infos = make(map[string]string)
@@ -140,7 +133,7 @@ func (p *Provider) BucketDump(ctx context.Context, action, bucketName string) {
140133
infos[b.BucketName] = b.Region
141134
}
142135
} else {
143-
infos[bucketName] = p.cfg.Region
136+
infos[bucketName] = p.defaultRegion
144137
}
145138
s3provider.ListObjects(ctx, infos)
146139
case "total":
@@ -155,7 +148,7 @@ func (p *Provider) BucketDump(ctx context.Context, action, bucketName string) {
155148
infos[b.BucketName] = b.Region
156149
}
157150
} else {
158-
infos[bucketName] = p.cfg.Region
151+
infos[bucketName] = p.defaultRegion
159152
}
160153
s3provider.TotalObjects(ctx, infos)
161154
default:

pkg/providers/aws/config.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,9 @@ package aws
33
import (
44
"strings"
55

6-
"github.com/404tk/cloudtoolkit/pkg/providers/aws/auth"
76
"github.com/404tk/cloudtoolkit/pkg/providers/aws/internal/arnutil"
8-
awsv2 "github.com/aws/aws-sdk-go-v2/aws"
97
)
108

11-
func newConfig(
12-
accessKey string,
13-
secretKey string,
14-
token string,
15-
region string,
16-
version string,
17-
) (awsv2.Config, error) {
18-
credential := auth.New(accessKey, secretKey, token)
19-
if err := credential.Validate(); err != nil {
20-
return awsv2.Config{}, err
21-
}
22-
return awsv2.Config{
23-
Region: resolveBootstrapRegion(region, version),
24-
Credentials: awsv2.NewCredentialsCache(credential),
25-
}, nil
26-
}
27-
289
func resolveBootstrapRegion(region string, version string) string {
2910
if region != "" && region != "all" {
3011
return region

pkg/providers/aws/config_test.go

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package aws
22

3-
import (
4-
"context"
5-
"testing"
6-
)
3+
import "testing"
74

85
func TestResolveBootstrapRegion(t *testing.T) {
96
tests := []struct {
@@ -57,23 +54,3 @@ func TestConsoleURLForARN(t *testing.T) {
5754
}
5855
}
5956
}
60-
61-
func TestNewConfigUsesStaticCredentials(t *testing.T) {
62-
cfg, err := newConfig("ak", "sk", "token", "all", "China")
63-
if err != nil {
64-
t.Fatalf("newConfig() error = %v", err)
65-
}
66-
if cfg.Region != "cn-northwest-1" {
67-
t.Fatalf("unexpected region: %s", cfg.Region)
68-
}
69-
if cfg.Credentials == nil {
70-
t.Fatal("expected credentials provider")
71-
}
72-
creds, err := cfg.Credentials.Retrieve(context.Background())
73-
if err != nil {
74-
t.Fatalf("Retrieve() error = %v", err)
75-
}
76-
if creds.AccessKeyID != "ak" || creds.SecretAccessKey != "sk" || creds.SessionToken != "token" {
77-
t.Fatalf("unexpected credentials: %+v", creds)
78-
}
79-
}

0 commit comments

Comments
 (0)