-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support retrieving the ec2 name through imds (#79)
- Loading branch information
1 parent
78d80c2
commit 2ea10d3
Showing
27 changed files
with
1,254 additions
and
229 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 |
---|---|---|
|
@@ -53,7 +53,7 @@ jobs: | |
- name: Setup Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: 1.18 | ||
go-version: 1.19 | ||
|
||
- name: Cache Go | ||
uses: actions/cache@v3 | ||
|
@@ -69,6 +69,7 @@ jobs: | |
uses: arduino/setup-task@v1 | ||
with: | ||
repo-token: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Test | ||
run: task test | ||
|
||
|
@@ -81,11 +82,14 @@ jobs: | |
if: matrix.os == 'ubuntu-latest' | ||
run: task integration-test | ||
|
||
# Temporary workaround until the golangci-lint GitHub action supports 1.19 | ||
- name: Install golangci-lint | ||
if: matrix.os == 'ubuntu-latest' | ||
run: go install github.com/golangci/golangci-lint/cmd/[email protected] | ||
|
||
- name: Lint Code | ||
if: matrix.os == 'ubuntu-latest' | ||
uses: golangci/[email protected] | ||
with: | ||
skip-go-installation: true | ||
run: golangci-lint run --version --verbose --out-format=github-actions | ||
|
||
- name: misspell | ||
if: matrix.os == 'ubuntu-latest' | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
Copyright (c) 2022 Purple Clay | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"strings" | ||
|
||
awsimds "github.com/aws/aws-sdk-go-v2/feature/ec2/imds" | ||
awsec2 "github.com/aws/aws-sdk-go-v2/service/ec2" | ||
"github.com/purpleclay/dns53/internal/ec2" | ||
"github.com/purpleclay/dns53/internal/imds" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// Custom type used to toggle any setting "on" or "off" | ||
type toggleSetting string | ||
|
||
const ( | ||
toggleSettingOn toggleSetting = "on" | ||
toggleSettingOff toggleSetting = "off" | ||
) | ||
|
||
func (t *toggleSetting) String() string { | ||
return string(*t) | ||
} | ||
|
||
func (t *toggleSetting) Set(v string) error { | ||
setting := strings.ToLower(v) | ||
|
||
switch setting { | ||
case "on", "off": | ||
*t = toggleSetting(setting) | ||
return nil | ||
default: | ||
return errors.New(`supported values are "on" or "off" (case-insensitive)`) | ||
} | ||
} | ||
|
||
func (t *toggleSetting) Type() string { | ||
return "string" | ||
} | ||
|
||
type imdsOptions struct { | ||
InstanceMetadataTags toggleSetting | ||
} | ||
|
||
func newIMDSCommand(out io.Writer) *cobra.Command { | ||
opt := imdsOptions{} | ||
|
||
imdsCmd := &cobra.Command{ | ||
Use: "imds", | ||
Short: "Toggle EC2 IMDS features", | ||
Args: cobra.NoArgs, | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cfg, err := awsConfig(globalOpts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if opt.InstanceMetadataTags == "" { | ||
return nil | ||
} | ||
|
||
return toggleMetadataTags(awsec2.NewFromConfig(cfg), awsimds.NewFromConfig(cfg), opt.InstanceMetadataTags) | ||
}, | ||
} | ||
|
||
f := imdsCmd.Flags() | ||
f.Var(&opt.InstanceMetadataTags, "instance-metadata-tags", "toggle the inclusion of EC2 instance tags within IMDS (on|off)") | ||
|
||
imdsCmd.MarkFlagRequired("--instance-metadata-tags") | ||
return imdsCmd | ||
} | ||
|
||
func toggleMetadataTags(ec2API ec2.ClientAPI, imdsAPI imds.ClientAPI, setting toggleSetting) error { | ||
ec2Client := ec2.NewFromAPI(ec2API) | ||
imdsClient := imds.NewFromAPI(imdsAPI) | ||
|
||
metadata, err := imdsClient.InstanceMetadata(context.Background()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var toggle ec2.InstanceMetadataToggle | ||
|
||
switch setting { | ||
case toggleSettingOn: | ||
toggle = ec2.InstanceMetadataToggleEnabled | ||
case toggleSettingOff: | ||
toggle = ec2.InstanceMetadataToggleDisabled | ||
} | ||
|
||
return ec2Client.ToggleInstanceMetadataTags(context.Background(), metadata.InstanceID, toggle) | ||
} |
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,116 @@ | ||
/* | ||
Copyright (c) 2022 Purple Clay | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/ec2" | ||
"github.com/aws/aws-sdk-go-v2/service/ec2/types" | ||
"github.com/purpleclay/dns53/internal/ec2/ec2mock" | ||
"github.com/purpleclay/dns53/internal/imds/imdsstub" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestToggleSettingString(t *testing.T) { | ||
toggle := toggleSetting("on") | ||
assert.Equal(t, "on", toggle.String()) | ||
} | ||
|
||
func TestToggleSettingSet(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
name: "LowercaseOn", | ||
input: "on", | ||
expected: "on", | ||
}, | ||
{ | ||
name: "LowercaseOff", | ||
input: "off", | ||
expected: "off", | ||
}, | ||
{ | ||
name: "MixedCaseOn", | ||
input: "oN", | ||
expected: "on", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var setting toggleSetting | ||
|
||
err := setting.Set(tt.input) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, tt.expected, string(setting)) | ||
}) | ||
} | ||
} | ||
|
||
func TestToggleSettingSetError(t *testing.T) { | ||
var setting toggleSetting | ||
|
||
err := setting.Set("not-supported") | ||
assert.EqualError(t, err, `supported values are "on" or "off" (case-insensitive)`) | ||
} | ||
|
||
func TestToggleSettingType(t *testing.T) { | ||
toggle := toggleSetting("on") | ||
assert.Equal(t, "string", toggle.Type()) | ||
} | ||
|
||
func TestToggleMetadataTags(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
toggle toggleSetting | ||
expected string | ||
}{ | ||
{ | ||
name: "On", | ||
toggle: toggleSettingOn, | ||
expected: "enabled", | ||
}, | ||
{ | ||
name: "Off", | ||
toggle: toggleSettingOff, | ||
expected: "disabled", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
mockEC2 := ec2mock.New(t) | ||
mockEC2.On("ModifyInstanceMetadataOptions", mock.Anything, mock.MatchedBy(func(req *ec2.ModifyInstanceMetadataOptionsInput) bool { | ||
return req.InstanceMetadataTags == types.InstanceMetadataTagsState(tt.expected) | ||
}), mock.Anything).Return(&ec2.ModifyInstanceMetadataOptionsOutput{}, nil) | ||
|
||
err := toggleMetadataTags(mockEC2, imdsstub.New(t), tt.toggle) | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
} |
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.