-
Notifications
You must be signed in to change notification settings - Fork 283
/
main.go
161 lines (136 loc) · 5.02 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
//
// Copyright The Athenz Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"flag"
"fmt"
"github.com/AthenZ/athenz/libs/go/sia/ssh/hostkey"
"github.com/AthenZ/athenz/libs/go/sia/util"
"log"
"os"
"strings"
"github.com/AthenZ/athenz/libs/go/sia/agent"
"github.com/AthenZ/athenz/libs/go/sia/gcp/meta"
"github.com/AthenZ/athenz/libs/go/sia/options"
"github.com/AthenZ/athenz/provider/gcp/sia-gce"
)
// Following can be set by the build script using LDFLAGS
var Version string
const siaMainDir = "/var/lib/sia"
const sshDir = "/etc/ssh"
func main() {
cmd := flag.String("cmd", "", "optional sub command to run")
gceMetaEndPoint := flag.String("meta", "http://169.254.169.254:80", "meta endpoint")
ztsEndPoint := flag.String("zts", "", "Athenz Token Service (ZTS) endpoint")
ztsServerName := flag.String("ztsservername", "", "ZTS server name for tls connections (optional)")
ztsCACert := flag.String("ztscacert", "", "Athenz Token Service (ZTS) CA certificate file (optional)")
dnsDomains := flag.String("dnsdomains", "", "DNS Domains associated with the provider")
ztsPort := flag.Int("ztsport", 4443, "Athenz Token Service (ZTS) port number")
pConf := flag.String("config", "/etc/sia/sia_config", "The config file to run against")
providerPrefix := flag.String("providerprefix", "", "Provider name prefix e.g athenz.gcp")
udsPath := flag.String("uds", "", "uds path")
noSysLog := flag.Bool("nosyslog", false, "turn off syslog, log to stdout")
displayVersion := flag.Bool("version", false, "Display version information")
accessProfileConf := flag.String("profileconfig", "/etc/sia/profile_config", "The user access management profile config file")
flag.Parse()
if *displayVersion {
fmt.Println(Version)
os.Exit(0)
}
if !*noSysLog {
sysLogger, err := util.NewSysLogger()
if err == nil {
log.SetOutput(sysLogger)
log.SetFlags(0)
} else {
log.SetFlags(log.LstdFlags)
log.Printf("Unable to create sys logger: %v\n", err)
}
} else {
log.SetFlags(log.LstdFlags)
}
if *ztsEndPoint == "" {
log.Fatalln("missing zts argument")
}
ztsUrl := fmt.Sprintf("https://%s:%d/zts/v1", *ztsEndPoint, *ztsPort)
if *dnsDomains == "" {
log.Fatalln("missing dnsdomains argument")
}
if *providerPrefix == "" {
log.Fatalln("missing providerprefix argument")
}
log.Printf("SIA-GCE version: %s \n", Version)
region := meta.GetRegion(*gceMetaEndPoint)
provider := sia.GCEProvider{
Name: fmt.Sprintf("%s.%s", *providerPrefix, region),
}
config, accessProfileConfig, err := sia.GetGCEConfig(*pConf, *accessProfileConf, *gceMetaEndPoint, region, provider)
if err != nil {
log.Fatalf("Unable to formulate configuration objects, error: %v\n", err)
}
// backward compatibility sake, keeping the ConfigAccount struct
configAccount := &options.ConfigAccount{
Name: fmt.Sprintf("%s.%s", config.Domain, config.Service),
User: config.User,
Group: config.Group,
Domain: config.Domain,
Account: config.Account,
Service: config.Service,
Zts: config.Zts,
Threshold: config.Threshold,
SshThreshold: config.SshThreshold,
Roles: config.Roles,
}
opts, err := options.NewOptions(config, configAccount, accessProfileConfig, siaMainDir, Version, false, region)
if err != nil {
log.Fatalf("Unable to formulate options, error: %v\n", err)
}
instanceId, err := meta.GetInstanceId(*gceMetaEndPoint)
if err != nil {
log.Fatalf("Unable to get instance id, error: %v\n", err)
}
// not being able to get an instance name is not a fatal error
instanceName, err := meta.GetInstanceName(*gceMetaEndPoint)
if err != nil {
instanceName = ""
log.Printf("Unable to get instance name, error: %v\n", err)
}
privateIp, err := meta.GetInstancePrivateIp(*gceMetaEndPoint)
if err != nil {
log.Fatalf("Unable to get instance private ip, error: %v\n", err)
}
if *udsPath != "" {
opts.SDSUdsPath = *udsPath
}
opts.MetaEndPoint = *gceMetaEndPoint
opts.Ssh = true
opts.ZTSCACertFile = *ztsCACert
opts.ZTSServerName = *ztsServerName
opts.ZTSCloudDomains = strings.Split(*dnsDomains, ",")
opts.InstanceId = instanceId
opts.InstanceName = instanceName
opts.Provider = provider
opts.PrivateIp = privateIp
opts.SpiffeNamespace = "default"
// Better defaults
opts.RotateKey = true
opts.GenerateRoleKey = true
opts.SshHostKeyType = hostkey.Ecdsa
opts.SshCertFile = hostkey.CertFile(sshDir, opts.SshHostKeyType)
opts.SshPubKeyFile = hostkey.PubKeyFile(sshDir, opts.SshHostKeyType)
agent.SetupAgent(opts, siaMainDir, "")
agent.RunAgent(*cmd, ztsUrl, opts)
}