Skip to content

Commit c937bb6

Browse files
committed
Add debug flag
1 parent 0a3bb24 commit c937bb6

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ You can optionally also include the following arguments to the `iamlive` command
7777

7878
**--account-id:** the AWS account ID to use in policy outputs within proxy mode (_default: 123456789012 unless detected_) (_AWS only_)
7979

80+
**--debug:** dumps associated HTTP requests when set in proxy mode (_default: false_)
81+
8082
_Basic Example (CSM Mode)_
8183

8284
```

iamlivecore/logger.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,20 @@ func writePolicyToTerminal() {
324324
}
325325

326326
policyDoc := string(GetPolicyDocument())
327-
policyHeight := countRune(policyDoc, '\n') + 1
328327

329-
goterm.Clear()
330-
goterm.MoveCursor(1, 1)
331-
if goterm.Height() < policyHeight {
332-
fmt.Println("\n\n" + policyDoc)
328+
if *debugFlag {
329+
fmt.Println(policyDoc)
333330
} else {
334-
goterm.Println(policyDoc)
335-
goterm.Flush()
331+
policyHeight := countRune(policyDoc, '\n') + 1
332+
333+
goterm.Clear()
334+
goterm.MoveCursor(1, 1)
335+
if goterm.Height() < policyHeight {
336+
fmt.Println("\n\n" + policyDoc)
337+
} else {
338+
goterm.Println(policyDoc)
339+
goterm.Flush()
340+
}
336341
}
337342
}
338343

iamlivecore/proxy.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"log"
1717
"math/big"
1818
"net/http"
19+
"net/http/httputil"
1920
"net/url"
2021
"os"
2122
"path/filepath"
@@ -160,6 +161,11 @@ func loadCAKeys() error {
160161
return nil
161162
}
162163

164+
func dumpReq(req *http.Request) {
165+
dump, _ := httputil.DumpRequestOut(req, true)
166+
fmt.Printf("%v\n", string(dump))
167+
}
168+
163169
func createProxy(addr string) {
164170
err := loadCAKeys()
165171
if err != nil {
@@ -170,19 +176,32 @@ func createProxy(addr string) {
170176
proxy.Logger = log.New(io.Discard, "", log.LstdFlags)
171177
proxy.OnRequest().HandleConnect(goproxy.AlwaysMitm)
172178
proxy.OnRequest().DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { // TODO: Move to onResponse for HTTP response codes
173-
body, _ := ioutil.ReadAll(req.Body)
179+
var body []byte
174180

175181
isAWSHostname, _ := regexp.MatchString(`^.*\.amazonaws\.com(?:\.cn)?$`, req.Host)
182+
isAzureHostname, _ := regexp.MatchString(`^(?:management\.azure\.com)|(?:management\.core\.windows\.net)$`, req.Host)
183+
isGCPHostname, _ := regexp.MatchString(`^.*\.googleapis\.com$`, req.Host)
184+
176185
if isAWSHostname && *providerFlag == "aws" {
186+
if *debugFlag {
187+
dumpReq(req)
188+
}
189+
body, _ = ioutil.ReadAll(req.Body)
177190
handleAWSRequest(req, body, 200)
178-
}
179-
isAzureHostname, _ := regexp.MatchString(`^(?:management\.azure\.com)|(?:management\.core\.windows\.net)$`, req.Host)
180-
if isAzureHostname && *providerFlag == "azure" {
191+
} else if isAzureHostname && *providerFlag == "azure" {
192+
if *debugFlag {
193+
dumpReq(req)
194+
}
195+
body, _ = ioutil.ReadAll(req.Body)
181196
handleAzureRequest(req, body, 200)
182-
}
183-
isGCPHostname, _ := regexp.MatchString(`^.*\.googleapis\.com$`, req.Host)
184-
if isGCPHostname && *providerFlag == "gcp" {
197+
} else if isGCPHostname && *providerFlag == "gcp" {
198+
if *debugFlag {
199+
dumpReq(req)
200+
}
201+
body, _ = ioutil.ReadAll(req.Body)
185202
handleGCPRequest(req, body, 200)
203+
} else {
204+
return req, nil
186205
}
187206

188207
req.Body = ioutil.NopCloser(bytes.NewBuffer(body))

iamlivecore/service.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var caBundleFlag *string
2727
var caKeyFlag *string
2828
var accountIDFlag *string
2929
var backgroundFlag *bool
30+
var debugFlag *bool
3031
var forceWildcardResourceFlag *bool
3132
var cpuProfileFlag = flag.String("cpu-profile", "", "write a CPU profile to this file (for performance testing purposes)")
3233

@@ -45,6 +46,7 @@ func parseConfig() {
4546
caKey := "~/.iamlive/ca.key"
4647
accountID := ""
4748
background := false
49+
debug := false
4850
forceWildcardResource := false
4951

5052
cfgfile, err := homedir.Expand("~/.iamlive/config")
@@ -93,6 +95,9 @@ func parseConfig() {
9395
if cfg.Section("").HasKey("background") {
9496
background, _ = cfg.Section("").Key("background").Bool()
9597
}
98+
if cfg.Section("").HasKey("debug") {
99+
debug, _ = cfg.Section("").Key("debug").Bool()
100+
}
96101
if cfg.Section("").HasKey("force-wildcard-resource") {
97102
forceWildcardResource, _ = cfg.Section("").Key("force-wildcard-resource").Bool()
98103
}
@@ -113,6 +118,7 @@ func parseConfig() {
113118
caKeyFlag = flag.String("ca-key", caKey, "the CA certificate key to use for proxy mode")
114119
accountIDFlag = flag.String("account-id", accountID, "the AWS account ID to use in policy outputs within proxy mode")
115120
backgroundFlag = flag.Bool("background", background, "when set, the process will return the current PID and run in the background without output")
121+
debugFlag = flag.Bool("debug", debug, "dumps associated HTTP requests when set in proxy mode")
116122
forceWildcardResourceFlag = flag.Bool("force-wildcard-resource", forceWildcardResource, "when set, the Resource will always be a wildcard")
117123
}
118124

@@ -169,7 +175,7 @@ func Run() {
169175
}
170176
}
171177

172-
func RunWithArgs(provider string, setIni bool, profile string, failsOnly bool, outputFile string, refreshRate int, sortAlphabetical bool, host, mode, bindAddr, caBundle, caKey, accountID string, background, forceWildcardResource bool) {
178+
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) {
173179
providerFlag = &provider
174180
setiniFlag = &setIni
175181
profileFlag = &profile
@@ -184,6 +190,7 @@ func RunWithArgs(provider string, setIni bool, profile string, failsOnly bool, o
184190
caKeyFlag = &caKey
185191
accountIDFlag = &accountID
186192
backgroundFlag = &background
193+
debugFlag = &debug
187194
forceWildcardResourceFlag = &forceWildcardResource
188195

189196
if *cpuProfileFlag != "" {

0 commit comments

Comments
 (0)