-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
174 lines (142 loc) · 4.36 KB
/
main.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
package main
import (
"embed"
"fmt"
"html/template"
"os"
"path/filepath"
"slices"
"strings"
"github.com/unicrons/steampipe-config-generator/cmd"
"github.com/unicrons/steampipe-config-generator/pkg/aws"
"github.com/unicrons/steampipe-config-generator/pkg/logger"
log "github.com/sirupsen/logrus"
)
//go:embed templates/*.tmpl
var templates embed.FS
type CredentialAccount struct {
Name string
RoleARN string
CredentialSource string
ImportSchema string
DefaultRegion string
TargetRegions []string
}
type ConnectionsTemplateData struct {
Accounts []CredentialAccount
Tags map[string][]string
}
const defaultTmplFile = "templates/aws_connections.tmpl"
func createAWSCredentialsFile(credentialPath string, organizationAccounts []CredentialAccount) error {
tmplFile := "templates/aws_credentials.tmpl"
t, err := template.ParseFS(templates, tmplFile)
if err != nil {
return fmt.Errorf("error parsing template: %v", err)
}
err = os.MkdirAll(credentialPath, os.ModePerm)
if err != nil {
return fmt.Errorf("error creating aws credentials path: %v", err)
}
filePath := filepath.Join(credentialPath, "credentials")
file, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("error creating aws credentials file: %v", err)
}
defer file.Close()
err = t.Execute(file, organizationAccounts)
if err != nil {
return fmt.Errorf("error executing template: %v", err)
}
log.Debug("AWS credentials file created in:", filePath)
return nil
}
func createAWSConnectionsFile(connectionsPath, templatePath string, data ConnectionsTemplateData) error {
t, err := parseTemplate(templatePath)
if err != nil {
return err
}
err = os.MkdirAll(connectionsPath, os.ModePerm)
if err != nil {
return fmt.Errorf("error creating aws credentials path: %v", err)
}
filePath := filepath.Join(connectionsPath, "aws.spc")
file, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("error creating aws connections file: %v", err)
}
defer file.Close()
err = t.Execute(file, data)
if err != nil {
return fmt.Errorf("error executing template: %v", err)
}
log.Debug("AWS Connections file created in:", filePath)
return nil
}
func parseTemplate(templatePath string) (*template.Template, error) {
if templatePath == "" {
return template.ParseFS(templates, defaultTmplFile)
} else {
return template.ParseFiles(templatePath)
}
}
func main() {
flags, err := cmd.ParseFlags()
if err != nil {
log.Error("error parsing flags:", err)
return
}
log.Debug("parsed flags:", flags)
roleName := flags.RoleName
credentialSource := flags.CredentialSource
credentialPath := flags.CredentialPath
connectionsPath := flags.ConnectionsPath
importSchema := flags.ImportSchema
defaultRegion := flags.DefaultRegion
targetRegions := flags.TargetRegions
assumeRoleArn := flags.AssumeRoleArn
templatePath := flags.TemplatePath
skipOUs := flags.SkipOUs
logFormat := flags.LogFormat
logger.SetLoggerFormat(logFormat)
accounts, err := aws.GetOrganizationAccounts(assumeRoleArn, defaultRegion)
if err != nil {
log.Error("error getting aws organization accounts:", err)
return
}
var organizationAccounts []CredentialAccount
taggedAccounts := make(map[string][]string)
for _, acc := range accounts {
if slices.Contains(skipOUs, acc.AccountOU) {
log.Infof("Skipping account %v included skipOUs argument", acc.AccountID)
continue
}
name := strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(acc.Name, " ", "_"), "-", "_"))
for key, value := range acc.Tags {
tagKey := key + "," + value
taggedAccounts[tagKey] = append(taggedAccounts[tagKey], name)
}
organizationAccounts = append(organizationAccounts, CredentialAccount{
Name: name,
RoleARN: "arn:aws:iam::" + acc.AccountID + ":role/" + roleName,
CredentialSource: credentialSource,
ImportSchema: importSchema,
DefaultRegion: defaultRegion,
TargetRegions: targetRegions,
})
}
data := ConnectionsTemplateData{
Accounts: organizationAccounts,
Tags: taggedAccounts,
}
err = createAWSCredentialsFile(credentialPath, organizationAccounts)
if err != nil {
log.Error("error creating aws credentials file:", err)
return
}
err = createAWSConnectionsFile(connectionsPath, templatePath, data)
if err != nil {
log.Error("error creating aws connections file:", err)
return
}
log.Info("config files created successfully")
}