-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial user-data commit * small changes in support of userdata PR #171 * Initial userdata test commit * some minor changes in support of userdata PR. Fix tests. * additional userdata test fixes. Unified userdata values. * updated comment Co-authored-by: Jim Shaver <elitest@Jims-MBP.lan> Co-authored-by: Jim Shaver <elitest@Jims-MBP.localdomain>
- Loading branch information
1 parent
33cad60
commit 0965dfc
Showing
12 changed files
with
291 additions
and
8 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
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,85 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
var ( | ||
udCfgPrefix = "userdata." | ||
udPathsCfgPrefix = udCfgPrefix + "paths." | ||
udValuesCfgPrefix = udCfgPrefix + "values." | ||
|
||
// mapping of udValue KEYS to udPath KEYS requiring path substitutions on override | ||
udValueToPlaceholderPathsKeyMap = map[string][]string{} | ||
|
||
// supported URL paths to run a mock | ||
udPathsDefaults = map[string]interface{}{} | ||
|
||
// values in mock responses | ||
udValuesDefaults = map[string]interface{}{} | ||
) | ||
|
||
// GetCfgUdValPrefix returns the prefix to use to access userdata values in config | ||
func GetCfgUdValPrefix() string { | ||
return udCfgPrefix + "." + udValuesCfgPrefix + "." | ||
} | ||
|
||
// GetCfgUdPathsPrefix returns the prefix to use to access userdata values in config | ||
func GetCfgUdPathsPrefix() string { | ||
return udCfgPrefix + "." + udPathsCfgPrefix + "." | ||
} | ||
|
||
// BindUserdataCfg binds a flag that represents a userdata value to configuration | ||
func BindUserdataCfg(flag *pflag.Flag) { | ||
bindFlagWithKeyPrefix(flag, udValuesCfgPrefix) | ||
} | ||
|
||
// SetUserdataDefaults sets config defaults for userdata paths and values | ||
func SetUserdataDefaults(jsonWithDefaults []byte) { | ||
// Unmarshal to map to preserve keys for Paths and Values | ||
var defaultsMap map[string]interface{} | ||
json.Unmarshal(jsonWithDefaults, &defaultsMap) | ||
udPaths := defaultsMap["userdata"].(map[string]interface{})["paths"].(map[string]interface{}) | ||
|
||
udValues := defaultsMap["userdata"].(map[string]interface{})["values"].(map[string]interface{}) | ||
|
||
for k, v := range udPaths { | ||
newKey := udPathsCfgPrefix + k | ||
// ex: "userdata": "/latest/user-data" | ||
udPathsDefaults[newKey] = v | ||
} | ||
|
||
for k, v := range udValues { | ||
newKey := udValuesCfgPrefix + k | ||
// ex: "userdata": "1234,john,reboot,true|4512,richard,|173,,," | ||
udValuesDefaults[newKey] = v | ||
} | ||
|
||
LoadConfigFromDefaults(udPathsDefaults) | ||
LoadConfigFromDefaults(udValuesDefaults) | ||
} | ||
|
||
// GetUserdataDefaults returns config defaults for userdata paths and values | ||
func GetUserdataDefaults() (map[string]interface{}, map[string]interface{}) { | ||
return udPathsDefaults, udValuesDefaults | ||
} | ||
|
||
// GetUserdataValueToPlaceholderPathsKeyMap returns collection of userdata values that are substituted into paths | ||
func GetUserdataValueToPlaceholderPathsKeyMap() map[string][]string { | ||
return udValueToPlaceholderPathsKeyMap | ||
} |
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,74 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package userdata | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"reflect" | ||
|
||
cfg "github.com/aws/amazon-ec2-metadata-mock/pkg/config" | ||
"github.com/aws/amazon-ec2-metadata-mock/pkg/mock/imdsv2" | ||
"github.com/aws/amazon-ec2-metadata-mock/pkg/server" | ||
) | ||
|
||
var ( | ||
supportedPaths = make(map[string]interface{}) | ||
response interface{} | ||
// ServicePath defines the userdata service path | ||
ServicePath = "/latest/user-data" | ||
) | ||
|
||
// Handler processes http requests | ||
func Handler(res http.ResponseWriter, req *http.Request) { | ||
log.Println("Received request to mock userdata:", req.URL.Path) | ||
|
||
if val, ok := supportedPaths[req.URL.Path]; ok { | ||
|
||
response = val | ||
} else { | ||
response = "Something went wrong with: " + req.URL.Path | ||
} | ||
server.FormatAndReturnOctetResponse(res, response.(string)) | ||
|
||
} | ||
|
||
// RegisterHandlers registers handlers for userdata paths | ||
func RegisterHandlers(config cfg.Config) { | ||
pathValues := reflect.ValueOf(config.Userdata.Paths) | ||
udValues := reflect.ValueOf(config.Userdata.Values) | ||
// Iterate over fields in config.Userdata.Paths to | ||
// determine intersections with config.Userdata.Values. | ||
// Intersections represent which paths and values to bind. | ||
for i := 0; i < pathValues.NumField(); i++ { | ||
pathFieldName := pathValues.Type().Field(i).Name | ||
udValueFieldName := udValues.FieldByName(pathFieldName) | ||
if udValueFieldName.IsValid() { | ||
path := pathValues.Field(i).Interface().(string) | ||
value := udValueFieldName.Interface() | ||
if path != "" && value != nil { | ||
// Ex: "/latest/meta-data/instance-id" : "i-1234567890abcdef0" | ||
supportedPaths[path] = value | ||
if config.Imdsv2Required { | ||
server.HandleFunc(path, imdsv2.ValidateToken(Handler)) | ||
} else { | ||
|
||
server.HandleFunc(path, Handler) | ||
} | ||
} else { | ||
log.Printf("There was an issue registering path %v with udValue: %v", path, value) | ||
} | ||
} | ||
} | ||
} |
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,59 @@ | ||
#! /usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
TEST_CONFIG_FILE="$SCRIPTPATH/testdata/aemm-config-integ.json" | ||
|
||
USERDATA_DEFAULT="1234,john,reboot,true|4512,richard,|173,,," | ||
USERDATA_OVERRIDDEN="1234" | ||
|
||
ROOT_PATH="http://$HOSTNAME:$AEMM_PORT" | ||
USERDATA_TEST_PATH="$ROOT_PATH/latest/user-data" | ||
|
||
function test_userdata_defaults() { | ||
pid=$1 | ||
test_url=$2 | ||
test_name=$3 | ||
tput setaf $BLUE | ||
health_check $test_url | ||
TOKEN=$(get_v2Token $MAX_TOKEN_TTL $AEMM_PORT) | ||
|
||
actual_userdata=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" $USERDATA_TEST_PATH) | ||
assert_value "$actual_userdata" $USERDATA_DEFAULT "userdata $test_name" | ||
|
||
clean_up $pid | ||
} | ||
|
||
function test_userdata_overrides() { | ||
pid=$1 | ||
test_url=$2 | ||
test_name=$3 | ||
tput setaf $BLUE | ||
health_check $test_url | ||
TOKEN=$(get_v2Token $MAX_TOKEN_TTL $AEMM_PORT) | ||
|
||
updated_userdata=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" $USERDATA_TEST_PATH) | ||
assert_value "$updated_userdata" $USERDATA_OVERRIDDEN "userdata $test_name" | ||
|
||
clean_up $pid | ||
} | ||
|
||
tput setaf $BLUE | ||
echo "======================================================================================================" | ||
echo "🥑 Starting userdata integration tests $METADATA_VERSION" | ||
echo "======================================================================================================" | ||
|
||
|
||
# userdata data defaults | ||
start_cmd=$(create_cmd $METADATA_VERSION --port $AEMM_PORT) | ||
$start_cmd & | ||
USERDATA_PID=$! | ||
test_userdata_defaults $USERDATA_PID $USERDATA_TEST_PATH $USERDATA_DEFAULT | ||
|
||
# userdata data overrides | ||
start_cmd=$(create_cmd $METADATA_VERSION --port $AEMM_PORT -c $TEST_CONFIG_FILE) | ||
$start_cmd & | ||
USERDATA_PID=$! | ||
test_userdata_overrides $USERDATA_PID $USERDATA_TEST_PATH $USERDATA_OVERRIDDEN | ||
|
||
exit $EXIT_CODE_TO_RETURN |
Oops, something went wrong.