Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --aws-redirect-host cli arg to allow using Iamlive with Localstack #78

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-redirect-host") {
awsRedirectHost = cfg.Section("").Key("aws-redirect-host").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