Skip to content

Commit

Permalink
Add --aws-redirect-host cli arg
Browse files Browse the repository at this point in the history
--aws-redirect-host cli arg sets the endpoint to which all aws requests will be redirected.

This is useful for localstack, all requests can be processed as normal but forwarded to a localstack instance instead of AWS.
  • Loading branch information
rulio committed Jun 27, 2024
1 parent e95134c commit ba716b9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 6 additions & 1 deletion iamlivecore/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func dumpReq(req *http.Request) {
fmt.Printf("%v\n", string(dump))
}

func createProxy(addr string) {
func createProxy(addr string, awsRedirectHost string) {
err := loadCAKeys()
if err != nil {
log.Fatal(err)
Expand All @@ -187,6 +187,11 @@ func createProxy(addr string) {
}
body, _ = ioutil.ReadAll(req.Body)
handleAWSRequest(req, body, 200)

if awsRedirectHost != "" {
req.URL.Host = awsRedirectHost
req.Host = awsRedirectHost
}
} else if isAzureHostname && *providerFlag == "azure" {
if *debugFlag {
dumpReq(req)
Expand Down
14 changes: 11 additions & 3 deletions iamlivecore/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var debugFlag *bool
var forceWildcardResourceFlag *bool
var cpuProfileFlag = flag.String("cpu-profile", "", "write a CPU profile to this file (for performance testing purposes)")
var csmPortFlag *int
var awsRedirectHostFlag *string

func parseConfig() {
provider := "aws"
Expand All @@ -50,6 +51,7 @@ func parseConfig() {
debug := false
forceWildcardResource := false
csmPort := 31000
awsRedirectHost := ""

cfgfile, err := homedir.Expand("~/.iamlive/config")
if err == nil {
Expand Down Expand Up @@ -103,6 +105,10 @@ func parseConfig() {
if cfg.Section("").HasKey("force-wildcard-resource") {
forceWildcardResource, _ = cfg.Section("").Key("force-wildcard-resource").Bool()
}
if cfg.Section("").HasKey("aws-redire4ct") {
awsRedirectHost = cfg.Section("").Key("aws-redirect").String()
}

}
}

Expand All @@ -123,6 +129,7 @@ func parseConfig() {
debugFlag = flag.Bool("debug", debug, "dumps associated HTTP requests when set in proxy mode")
forceWildcardResourceFlag = flag.Bool("force-wildcard-resource", forceWildcardResource, "when set, the Resource will always be a wildcard")
csmPortFlag = flag.Int("csm-port", csmPort, "port to listen on for CSM")
awsRedirectHostFlag = flag.String("aws-redirect-host", awsRedirectHost, "redirect all AWS API calls to this endpoint")
}

func Run() {
Expand Down Expand Up @@ -172,13 +179,13 @@ func Run() {
handleLoggedCall()
} else if *modeFlag == "proxy" {
readServiceFiles()
createProxy(*bindAddrFlag)
createProxy(*bindAddrFlag, *awsRedirectHostFlag)
} else {
fmt.Println("ERROR: unknown mode")
}
}

func RunWithArgs(provider string, setIni bool, profile string, failsOnly bool, outputFile string, refreshRate int, sortAlphabetical bool, host, mode, bindAddr, caBundle, caKey, accountID string, background, debug, forceWildcardResource bool) {
func RunWithArgs(provider string, setIni bool, profile string, failsOnly bool, outputFile string, refreshRate int, sortAlphabetical bool, host, mode, bindAddr, caBundle, caKey, accountID string, background, debug, forceWildcardResource bool, awsRedirectHost string) {
providerFlag = &provider
setiniFlag = &setIni
profileFlag = &profile
Expand All @@ -195,6 +202,7 @@ func RunWithArgs(provider string, setIni bool, profile string, failsOnly bool, o
backgroundFlag = &background
debugFlag = &debug
forceWildcardResourceFlag = &forceWildcardResource
awsRedirectHostFlag = &awsRedirectHost

if *cpuProfileFlag != "" {
f, err := os.Create(*cpuProfileFlag)
Expand All @@ -220,7 +228,7 @@ func RunWithArgs(provider string, setIni bool, profile string, failsOnly bool, o
handleLoggedCall()
} else if *modeFlag == "proxy" {
readServiceFiles()
createProxy(*bindAddrFlag)
createProxy(*bindAddrFlag, *awsRedirectHostFlag)
} else {
fmt.Println("ERROR: unknown mode")
}
Expand Down

0 comments on commit ba716b9

Please sign in to comment.