-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
136 lines (116 loc) · 3.96 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
package main
import (
"encoding/json"
"flag"
"os"
"path"
"strings"
"github.com/deepfence/ThreatMapper/deepfence_utils/log"
"github.com/deepfence/ThreatMapper/deepfence_utils/utils"
cloudmetadata "github.com/deepfence/cloud-scanner/cloud-metadata"
"github.com/deepfence/cloud-scanner/internal/deepfence"
"github.com/deepfence/cloud-scanner/service"
"github.com/deepfence/cloud-scanner/util"
"github.com/kelseyhightower/envconfig"
)
var (
socketPath = flag.String("socket-path", "", "Path to socket")
)
var Version string
func runServices(config util.Config, socketPath *string) {
svc, err := service.NewComplianceScanService(config, socketPath)
if err != nil {
log.Error().Msgf("Error: %v", err)
return
}
log.Info().Msgf("Registering with Deepfence management console")
err = svc.RunRegisterServices()
if err != nil {
log.Fatal().Msgf("Error: %v", err)
}
}
func main() {
log.Info().Msgf("Starting cloud scanner, version: %s", Version)
flag.Parse()
if *socketPath == "" {
log.Fatal().Msgf("socket-path is not set")
}
var config util.Config
err := envconfig.Process("", &config)
if err != nil {
log.Fatal().Msg(err.Error())
}
err = log.Initialize(config.LogLevel)
if err != nil {
log.Fatal().Msg(err.Error())
}
if config.CloudProvider != "" && config.AccountID != "" && config.CloudRegion != "" {
config.CloudMetadata = cloudmetadata.CloudMetadata{
CloudProvider: config.CloudProvider,
ID: config.AccountID,
Region: config.CloudRegion,
}
} else {
config.CloudMetadata, err = util.GetCloudMetadata()
if err != nil {
log.Fatal().Msg(err.Error())
}
config.CloudProvider = config.CloudMetadata.CloudProvider
if config.CloudMetadata.ID != "" {
config.AccountID = config.CloudMetadata.ID
}
if config.CloudMetadata.Region != "" {
config.CloudRegion = config.CloudMetadata.Region
}
}
if config.AccountID == "" {
log.Fatal().Msgf("unable to retrieve account ID from metadata service, please set env CLOUD_ACCOUNT_ID")
}
if config.CloudProvider != util.CloudProviderAWS && config.CloudProvider != util.CloudProviderGCP && config.CloudProvider != util.CloudProviderAzure {
log.Fatal().Msgf("invalid CLOUD_PROVIDER - should be one of aws, azure, gcp")
}
if config.SuccessSignalUrl != "" {
deepfence.SendSuccessfulDeploymentSignal(config.SuccessSignalUrl)
}
if config.IsOrganizationDeployment {
if config.OrganizationID == "" {
log.Fatal().Msgf("CLOUD_ORGANIZATION_ID is required in organization deployment")
}
}
if config.DeployedAccountID == "" {
config.DeployedAccountID = config.AccountID
}
switch config.CloudProvider {
case util.CloudProviderAWS:
if config.AWSCredentialSource != "EcsContainer" &&
config.AWSCredentialSource != "Ec2InstanceMetadata" &&
config.AWSCredentialSource != "Environment" &&
config.AWSCredentialSource != "ServiceAccount" {
log.Fatal().Msgf("invalid AWS_CREDENTIAL_SOURCE - should be one of EcsContainer, Ec2InstanceMetadata, Environment, ServiceAccount")
}
if config.IsOrganizationDeployment && config.RoleName == "" {
log.Fatal().Msgf("ROLE_NAME is required in aws installation")
}
case util.CloudProviderGCP:
config.GCPCredentials = strings.TrimSpace(config.GCPCredentials)
default:
config.AWSCredentialSource = ""
}
config.NodeID = util.GetNodeID(config.CloudProvider, config.AccountID)
config.Version = Version
config.DatabasePersistenceSupported = config.DeploymentMode == util.DeploymentModeKubernetes || config.DeploymentMode == util.DeploymentModeDocker
fileContent, err := os.ReadFile(path.Join(util.InstallDirectory, ".install_id"))
if err != nil {
log.Fatal().Msg(err.Error())
return
}
config.InstallationID = strings.TrimSpace(strings.ReplaceAll(string(fileContent), "\n", ""))
if config.InstallationID == "" {
config.InstallationID = utils.NewUUIDString()
}
configJson, err := json.MarshalIndent(config, "", "\t")
if err == nil {
log.Info().Msgf("Using config: %s", string(configJson))
}
runServices(config, socketPath)
}