-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6589278
commit bad6b71
Showing
4 changed files
with
177 additions
and
36 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,141 @@ | ||
package ec2connect | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/request" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/aws/aws-sdk-go/service/ec2/ec2iface" | ||
"github.com/aws/aws-sdk-go/service/ec2instanceconnect" | ||
"github.com/aws/aws-sdk-go/service/ec2instanceconnect/ec2instanceconnectiface" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"io/ioutil" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestNormalizeKey(t *testing.T) { | ||
key := `-----BEGIN OPENSSH PRIVATE KEY----- | ||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFwAAAAdzc2gtcn | ||
NhAAAAAwEAAQAAAQEA6h0I+mglxqIfgn8h4cJigVBZdmLDHu8848c6Tpf+byGTeLvs6iKX | ||
GqIpdFFKlSIO0qIP42p1x8jkCnjuftMzmKyE4f3Kl1DBH0J4SKgtCUhOVh68rsV2QDHPD7 | ||
XG8elRQwNgz4rNpPVGVwEA1Kcdxf8mgzXc1rn1HooS2iMVjVCGzqnVl5yd3khnm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VWmDPUjBgqiWZ0lmT890ZAEZLb3Ie3PGSONiC9x4KXExXNa+QvsGZYTAVn | ||
9dslchTNLysb4AAACBAP6WhoN6zlZ0KGz7ziqYRoy4nJvsfPudyP1ARvmzSvKDuMYErYM4 | ||
cGbLoAZsnbI+77qUgz9CicJSeUVUASELnsQ2HMOhyDqeUm9P0KMbjhaTFMeE/WQOCQBBBH | ||
T4h8F282GSKygTjppZ0ClSI/tiFM3jB5Nj3r069A8jrUFUSb+3AAAAgQDraXBhOOrfT9VB | ||
/MWlvhkbbLGklR4MzxJ5PZyNh5ocnJs2137dreIeRpifFM54uADQpJpbiZgaEetrD4/+36 | ||
GykV+X17YB2yWLu7tE9AnXjE3n6qYnaC8nUprTCPeyfoREh1BRXSeARQ+Z9wdvD4iOQd7j | ||
9aCsICbXzY5+GmtGiwAAABxhaWRhbi5zdGVlbGVATE0tQzAyVjUxVkRIVEQ2AQIDBAU= | ||
-----END OPENSSH PRIVATE KEY----- | ||
` | ||
normal, err := NormalizeKey(key) | ||
assert.NoError(t, err) | ||
|
||
expected := `ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDqHQj6aCXGoh+CfyHhwmKBUFl2YsMe7zzjxzpOl/5vIZN4u+zqIpcaoil0UUqVIg7Sog/janXHyOQKeO5+0zOYrITh/cqXUMEfQnhIqC0JSE5WHryuxXZAMc8Ptcbx6VFDA2DPis2k9UZXAQDUpx3F/yaDNdzWufUeihLaIxWNUIbOqdWXnJ3eSGeZfxi1kUkP21Mjnx1vbJ6yQ0G/ozxLTwEx/TiHzGDbW4ScBqFIVWsJ0feXTupX/kwIdsgGB4XooSA8e22LwB/PbIEc3ag7K8lFy/jy6cJc6muwOhhiGPzyYM7cheUjwRzpixjIVIeVkXyM3wr5TaHLheKl9CJd` | ||
assert.Equal(t, expected, strings.TrimSpace(normal)) | ||
t.Run("valid", func(t *testing.T) { | ||
key, err := ioutil.ReadFile("testdata/id_rsa") | ||
assert.NoError(t, err) | ||
|
||
normal, err := NormalizeKey(string(key)) | ||
assert.NoError(t, err) | ||
|
||
expected := `ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDqHQj6aCXGoh+CfyHhwmKBUFl2YsMe7zzjxzpOl/5vIZN4u+zqIpcaoil0UUqVIg7Sog/janXHyOQKeO5+0zOYrITh/cqXUMEfQnhIqC0JSE5WHryuxXZAMc8Ptcbx6VFDA2DPis2k9UZXAQDUpx3F/yaDNdzWufUeihLaIxWNUIbOqdWXnJ3eSGeZfxi1kUkP21Mjnx1vbJ6yQ0G/ozxLTwEx/TiHzGDbW4ScBqFIVWsJ0feXTupX/kwIdsgGB4XooSA8e22LwB/PbIEc3ag7K8lFy/jy6cJc6muwOhhiGPzyYM7cheUjwRzpixjIVIeVkXyM3wr5TaHLheKl9CJd` | ||
assert.Equal(t, expected, strings.TrimSpace(normal)) | ||
}) | ||
|
||
t.Run("invalid", func(t *testing.T) { | ||
_, err := NormalizeKey("abc") | ||
assert.Error(t, err) | ||
}) | ||
} | ||
|
||
func TestAuthorizer_Authorize(t *testing.T) { | ||
t.Run("err describing", func(t *testing.T) { | ||
connApi := &mockConnect{} | ||
ec2Api := &mockEc2{} | ||
ec2Api. | ||
On("DescribeInstancesWithContext", mock.Anything, mock.AnythingOfType("*ec2.DescribeInstancesInput"), mock.AnythingOfType("[]request.Option")). | ||
Return(nil, errors.New("err")) | ||
|
||
auth := &Authorizer{Ec2Api: ec2Api, ConnectApi: connApi} | ||
_, err := auth.Authorize(context.Background(), "i-012abc", "", "") | ||
assert.Error(t, err) | ||
}) | ||
|
||
t.Run("no instances", func(t *testing.T) { | ||
connApi := &mockConnect{} | ||
ec2Api := &mockEc2{} | ||
ec2Api. | ||
On("DescribeInstancesWithContext", mock.Anything, mock.AnythingOfType("*ec2.DescribeInstancesInput"), mock.AnythingOfType("[]request.Option")). | ||
Return(&ec2.DescribeInstancesOutput{}, nil) | ||
|
||
auth := &Authorizer{Ec2Api: ec2Api, ConnectApi: connApi} | ||
_, err := auth.Authorize(context.Background(), "i-012abc", "", "") | ||
assert.Error(t, err) | ||
}) | ||
|
||
t.Run("err sending ssh key", func(t *testing.T) { | ||
ec2Api := &mockEc2{} | ||
ec2Api. | ||
On("DescribeInstancesWithContext", mock.Anything, mock.AnythingOfType("*ec2.DescribeInstancesInput"), mock.AnythingOfType("[]request.Option")). | ||
Return(&ec2.DescribeInstancesOutput{ | ||
Reservations: []*ec2.Reservation{ | ||
{ | ||
Instances: []*ec2.Instance{ | ||
{ | ||
Placement: &ec2.Placement{ | ||
AvailabilityZone: aws.String("ap-southeast-2b"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, nil) | ||
|
||
connApi := &mockConnect{} | ||
connApi. | ||
On("SendSSHPublicKeyWithContext", mock.Anything, mock.AnythingOfType("*ec2instanceconnect.SendSSHPublicKeyInput"), mock.AnythingOfType("[]request.Option")). | ||
Return(nil, errors.New("err")) | ||
|
||
auth := &Authorizer{Ec2Api: ec2Api, ConnectApi: connApi} | ||
_, err := auth.Authorize(context.Background(), "i-012abc", "", "") | ||
assert.Error(t, err) | ||
}) | ||
|
||
t.Run("unsuccessful send ssh key", func(t *testing.T) { | ||
ec2Api := &mockEc2{} | ||
ec2Api. | ||
On("DescribeInstancesWithContext", mock.Anything, mock.AnythingOfType("*ec2.DescribeInstancesInput"), mock.AnythingOfType("[]request.Option")). | ||
Return(&ec2.DescribeInstancesOutput{ | ||
Reservations: []*ec2.Reservation{ | ||
{ | ||
Instances: []*ec2.Instance{ | ||
{ | ||
Placement: &ec2.Placement{ | ||
AvailabilityZone: aws.String("ap-southeast-2b"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, nil) | ||
|
||
connApi := &mockConnect{} | ||
connApi. | ||
On("SendSSHPublicKeyWithContext", mock.Anything, mock.AnythingOfType("*ec2instanceconnect.SendSSHPublicKeyInput"), mock.AnythingOfType("[]request.Option")). | ||
Return(&ec2instanceconnect.SendSSHPublicKeyOutput{ | ||
Success: aws.Bool(false), | ||
}, nil) | ||
|
||
auth := &Authorizer{Ec2Api: ec2Api, ConnectApi: connApi} | ||
_, err := auth.Authorize(context.Background(), "i-012abc", "", "") | ||
assert.Error(t, err) | ||
}) | ||
} | ||
|
||
type mockEc2 struct { | ||
mock.Mock | ||
ec2iface.EC2API | ||
} | ||
|
||
func (m *mockEc2) DescribeInstancesWithContext(ctx aws.Context, input *ec2.DescribeInstancesInput, opts ...request.Option) (*ec2.DescribeInstancesOutput, error) { | ||
f := m.Called(ctx, input, opts) | ||
output, _ := f.Get(0).(*ec2.DescribeInstancesOutput) | ||
return output, f.Error(1) | ||
} | ||
|
||
type mockConnect struct { | ||
mock.Mock | ||
ec2instanceconnectiface.EC2InstanceConnectAPI | ||
} | ||
|
||
func (m *mockConnect) SendSSHPublicKeyWithContext(ctx aws.Context, input *ec2instanceconnect.SendSSHPublicKeyInput, opts ...request.Option) (*ec2instanceconnect.SendSSHPublicKeyOutput, error) { | ||
f := m.Called(ctx, input, opts) | ||
output, _ := f.Get(0).(*ec2instanceconnect.SendSSHPublicKeyOutput) | ||
return output, f.Error(1) | ||
} |
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,27 @@ | ||
-----BEGIN OPENSSH PRIVATE KEY----- | ||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFwAAAAdzc2gtcn | ||
NhAAAAAwEAAQAAAQEA6h0I+mglxqIfgn8h4cJigVBZdmLDHu8848c6Tpf+byGTeLvs6iKX | ||
GqIpdFFKlSIO0qIP42p1x8jkCnjuftMzmKyE4f3Kl1DBH0J4SKgtCUhOVh68rsV2QDHPD7 | ||
XG8elRQwNgz4rNpPVGVwEA1Kcdxf8mgzXc1rn1HooS2iMVjVCGzqnVl5yd3khnmX8YtZFJ | ||
D9tTI58db2yeskNBv6M8S08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