-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
csm.go
204 lines (179 loc) · 4.56 KB
/
csm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/signal"
"runtime/pprof"
"strings"
"syscall"
"github.com/mitchellh/go-homedir"
)
func setConfigKey(filename, section, line string, unset bool) error {
fileinfo, err := os.Stat(filename)
if err != nil {
return err
}
file, err := os.OpenFile(filename, os.O_RDONLY, fileinfo.Mode().Perm())
if err != nil {
return err
}
defer file.Close()
var newLines []string
isCorrectSection := false
scanner := bufio.NewScanner(file)
for scanner.Scan() {
scannedLine := scanner.Text()
if scannedLine != line || !unset || !isCorrectSection { // write all but lines to be removed
newLines = append(newLines, scannedLine)
}
if scannedLine == fmt.Sprintf("[%s]", section) {
isCorrectSection = true
if !unset {
newLines = append(newLines, line)
}
}
}
if err := scanner.Err(); err != nil {
return err
}
file.Close()
file, err = os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fileinfo.Mode().Perm())
if err != nil {
return err
}
defer file.Close()
writer := bufio.NewWriter(file)
for _, line := range newLines {
fmt.Fprintln(writer, line)
}
return writer.Flush()
}
func setINIConfigAndFileFlush() {
// set ini
if *setiniFlag {
cfgfile, err := homedir.Expand("~/.aws/config")
if err != nil {
log.Fatal(err)
}
if *profileFlag == "default" {
if *modeFlag == "csm" {
err = setConfigKey(cfgfile, "default", "csm_enabled = true", false)
} else if *modeFlag == "proxy" {
caBundlePath, err := homedir.Expand(*caBundleFlag)
if err != nil {
log.Fatal(err)
}
err = setConfigKey(cfgfile, "default", fmt.Sprintf("ca_bundle = %s", caBundlePath), false)
}
} else {
if *modeFlag == "csm" {
err = setConfigKey(cfgfile, fmt.Sprintf("profile %s", *profileFlag), "csm_enabled = true", false)
} else if *modeFlag == "proxy" {
caBundlePath, err := homedir.Expand(*caBundleFlag)
if err != nil {
log.Fatal(err)
}
err = setConfigKey(cfgfile, fmt.Sprintf("profile %s", *profileFlag), fmt.Sprintf("ca_bundle = %s", caBundlePath), false)
}
}
if err != nil {
log.Fatal(err)
}
}
// listen for exit, cleanup and flush
sigc := make(chan os.Signal, 1)
signal.Notify(sigc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
for s := range sigc {
// flush to file
if *outputFileFlag != "" {
err := ioutil.WriteFile(*outputFileFlag, getPolicyDocument(), 0644)
if err != nil {
log.Fatalf("Error writing policy to %s", *outputFileFlag)
}
}
if s == syscall.SIGINT || s == syscall.SIGTERM || s == syscall.SIGQUIT {
if *setiniFlag {
// revert ini
cfgfile, err := homedir.Expand("~/.aws/config") // need to redeclare
if err != nil {
os.Exit(1)
}
if *profileFlag == "default" {
if *modeFlag == "csm" {
setConfigKey(cfgfile, "default", "csm_enabled = true", true)
} else if *modeFlag == "proxy" {
caBundlePath, _ := homedir.Expand(*caBundleFlag)
setConfigKey(cfgfile, "default", fmt.Sprintf("ca_bundle = %s", caBundlePath), true)
}
} else {
if *modeFlag == "csm" {
setConfigKey(cfgfile, fmt.Sprintf("profile %s", *profileFlag), "csm_enabled = true", true)
} else if *modeFlag == "proxy" {
caBundlePath, _ := homedir.Expand(*caBundleFlag)
setConfigKey(cfgfile, fmt.Sprintf("profile %s", *profileFlag), fmt.Sprintf("ca_bundle = %s", caBundlePath), true)
}
}
}
pprof.StopCPUProfile()
// exit
os.Exit(0)
}
}
}()
}
func listenForEvents() {
var iamMap iamMapBase
addr := net.UDPAddr{
Port: 31000,
IP: net.ParseIP(*hostFlag),
}
conn, err := net.ListenUDP("udp", &addr)
if err != nil {
panic(err)
}
err = conn.SetReadBuffer(1048576)
if err != nil {
panic(err)
}
defer conn.Close()
err = json.Unmarshal(bIAMMap, &iamMap)
if err != nil {
panic(err)
}
var buf [1048576]byte
for {
rlen, _, err := conn.ReadFromUDP(buf[:])
if err != nil {
panic(err)
}
entries := strings.Split(string(buf[0:rlen]), "\n")
EntryLoop:
for _, entry := range entries {
var e Entry
err := json.Unmarshal([]byte(entry), &e)
if err != nil {
panic(err)
}
// checked if permissionless
for _, permissionlessAction := range iamMap.SDKPermissionlessActions {
if strings.ToLower(permissionlessAction) == fmt.Sprintf("%s.%s", strings.ToLower(e.Service), strings.ToLower(e.Method)) {
continue EntryLoop
}
}
if e.Type == "ApiCall" {
callLog = append(callLog, e)
handleLoggedCall()
}
}
}
}