Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e7ffc24

Browse files
committedJun 7, 2016
Detect platform and get instance-id of amazon ec2
1 parent 259f23f commit e7ffc24

File tree

3 files changed

+112
-5
lines changed

3 files changed

+112
-5
lines changed
 

‎models/models.go

+11
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ type ScanResult struct {
8282

8383
Container Container
8484

85+
Platform Platform
86+
8587
// Fqdn string
8688
// NWLinks []NWLink
8789
KnownCves []CveInfo
@@ -322,3 +324,12 @@ type Container struct {
322324
ContainerID string
323325
Name string
324326
}
327+
328+
// Platform has platform information
329+
type Platform struct {
330+
gorm.Model `json:"-"`
331+
ScanResultID uint `json:"-"`
332+
333+
Name string // aws or azure or gcp or other...
334+
InstanceID string
335+
}

‎scan/linux.go

+64-4
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ import (
3131
type linux struct {
3232
ServerInfo config.ServerInfo
3333

34-
Family string
35-
Release string
34+
Family string
35+
Release string
36+
Platform models.Platform
3637
osPackages
37-
log *logrus.Entry
3838

39+
log *logrus.Entry
3940
errs []error
4041
}
4142

@@ -56,10 +57,18 @@ func (l *linux) setDistributionInfo(fam, rel string) {
5657
l.Release = rel
5758
}
5859

59-
func (l *linux) getDistributionInfo() string {
60+
func (l linux) getDistributionInfo() string {
6061
return fmt.Sprintf("%s %s", l.Family, l.Release)
6162
}
6263

64+
func (l *linux) setPlatform(p models.Platform) {
65+
l.Platform = p
66+
}
67+
68+
func (l linux) getPlatform() models.Platform {
69+
return l.Platform
70+
}
71+
6372
func (l linux) allContainers() (containers []config.Container, err error) {
6473
switch l.ServerInfo.Container.Type {
6574
case "", "docker":
@@ -131,6 +140,56 @@ func (l *linux) parseDockerPs(stdout string) (containers []config.Container, err
131140
return
132141
}
133142

143+
func (l *linux) detectPlatform() error {
144+
ok, instanceID, err := l.detectRunningOnAws()
145+
if err != nil {
146+
return err
147+
}
148+
if ok {
149+
l.setPlatform(models.Platform{
150+
Name: "aws",
151+
InstanceID: instanceID,
152+
})
153+
return nil
154+
}
155+
156+
//TODO Azure, GCP...
157+
l.setPlatform(models.Platform{
158+
Name: "other",
159+
})
160+
return nil
161+
}
162+
163+
func (l linux) detectRunningOnAws() (ok bool, instanceID string, err error) {
164+
if r := l.ssh("type curl", noSudo); r.isSuccess() {
165+
cmd := "curl --max-time 1 --retry 3 --noproxy 169.254.169.254 http://169.254.169.254/latest/meta-data/instance-id"
166+
if r := l.ssh(cmd, noSudo); r.isSuccess() {
167+
id := strings.TrimSpace(r.Stdout)
168+
return true, id, nil
169+
} else if r.ExitStatus == 28 || r.ExitStatus == 7 {
170+
// Not running on AWS
171+
// 7 Failed to connect to host.
172+
// 28 operation timeout.
173+
return false, "", nil
174+
}
175+
}
176+
177+
if r := l.ssh("type wget", noSudo); r.isSuccess() {
178+
cmd := "wget --tries=3 --timeout=1 --no-proxy -q -O - http://169.254.169.254/latest/meta-data/instance-id"
179+
if r := l.ssh(cmd, noSudo); r.isSuccess() {
180+
id := strings.TrimSpace(r.Stdout)
181+
return true, id, nil
182+
} else if r.ExitStatus == 4 {
183+
// Not running on AWS
184+
// 4 Network failure
185+
return false, "", nil
186+
}
187+
}
188+
return false, "", fmt.Errorf(
189+
"Failed to curl or wget to AWS instance metadata on %s. container: %s",
190+
l.ServerInfo.ServerName, l.ServerInfo.Container.Name)
191+
}
192+
134193
func (l *linux) convertToModel() (models.ScanResult, error) {
135194
var scoredCves, unscoredCves models.CveInfos
136195
for _, p := range l.UnsecurePackages {
@@ -171,6 +230,7 @@ func (l *linux) convertToModel() (models.ScanResult, error) {
171230
Family: l.Family,
172231
Release: l.Release,
173232
Container: container,
233+
Platform: l.Platform,
174234
KnownCves: scoredCves,
175235
UnknownCves: unscoredCves,
176236
}, nil

‎scan/serverapi.go

+37-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ var Log *logrus.Entry
1515

1616
var servers []osTypeInterface
1717

18-
// Base Interface of redhat, debian
18+
// Base Interface of redhat, debian, freebsd
1919
type osTypeInterface interface {
2020
setServerInfo(config.ServerInfo)
2121
getServerInfo() config.ServerInfo
22+
2223
setDistributionInfo(string, string)
2324
getDistributionInfo() string
25+
26+
detectPlatform() error
27+
getPlatform() models.Platform
28+
2429
checkRequiredPackagesInstalled() error
2530
scanPackages() error
2631
scanVulnByCpeName() error
@@ -136,6 +141,30 @@ func InitServers(localLogger *logrus.Entry) error {
136141
return fmt.Errorf("Failed to detect Container OSes. err: %s", err)
137142
}
138143
servers = append(servers, containers...)
144+
145+
Log.Info("Detecting Platforms...")
146+
errs := detectPlatforms()
147+
if 0 < len(errs) {
148+
// Only logging
149+
Log.Errorf("Failed to detect platforms. err: %v", errs)
150+
}
151+
for i, s := range servers {
152+
if s.getServerInfo().IsContainer() {
153+
Log.Infof("(%d/%d) %s on %s is running on %s",
154+
i+1, len(servers),
155+
s.getServerInfo().Container.Name,
156+
s.getServerInfo().ServerName,
157+
s.getPlatform().Name,
158+
)
159+
160+
} else {
161+
Log.Infof("(%d/%d) %s is running on %s",
162+
i+1, len(servers),
163+
s.getServerInfo().ServerName,
164+
s.getPlatform().Name,
165+
)
166+
}
167+
}
139168
return nil
140169
}
141170

@@ -328,6 +357,13 @@ func detectContainerOSesOnServer(containerHost osTypeInterface) (oses []osTypeIn
328357
return oses
329358
}
330359

360+
func detectPlatforms() []error {
361+
timeoutSec := 1 * 60
362+
return parallelSSHExec(func(o osTypeInterface) error {
363+
return o.detectPlatform()
364+
}, timeoutSec)
365+
}
366+
331367
// Prepare installs requred packages to scan vulnerabilities.
332368
func Prepare() []error {
333369
return parallelSSHExec(func(o osTypeInterface) error {

0 commit comments

Comments
 (0)
Please sign in to comment.