This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f51fe2
commit 7c77fce
Showing
8 changed files
with
352 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package amazonlinux | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/knqyf263/fanal/analyzer/os" | ||
|
||
"github.com/knqyf263/fanal/analyzer" | ||
"github.com/knqyf263/fanal/extractor" | ||
) | ||
|
||
func init() { | ||
analyzer.RegisterOSAnalyzer(&amazonlinuxOSAnalyzer{}) | ||
} | ||
|
||
type amazonlinuxOSAnalyzer struct{} | ||
|
||
func (a amazonlinuxOSAnalyzer) Analyze(fileMap extractor.FileMap) (analyzer.OS, error) { | ||
for _, filename := range a.RequiredFiles() { | ||
file, ok := fileMap[filename] | ||
if !ok { | ||
continue | ||
} | ||
scanner := bufio.NewScanner(bytes.NewBuffer(file)) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
fields := strings.Fields(line) | ||
// Amazon Linuxという名称が確認できたときのみ | ||
if strings.HasPrefix(line, "Amazon Linux release 2") { | ||
return analyzer.OS{ | ||
Family: os.Amazon, | ||
Name: fmt.Sprintf("%s %s", fields[3], fields[4]), | ||
}, nil | ||
} else if strings.HasPrefix(line, "Amazon Linux") { | ||
return analyzer.OS{ | ||
Family: os.Amazon, | ||
Name: strings.Join(fields[2:], " "), | ||
}, nil | ||
} | ||
} | ||
} | ||
return analyzer.OS{}, errors.New("amzn: Not match") | ||
} | ||
|
||
func (a amazonlinuxOSAnalyzer) RequiredFiles() []string { | ||
return []string{"etc/system-release"} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package os | ||
|
||
const ( | ||
// RedHat is done | ||
RedHat = "redhat" | ||
|
||
// Debian is done | ||
Debian = "debian" | ||
|
||
// Ubuntu is done | ||
Ubuntu = "ubuntu" | ||
|
||
// CentOS is done | ||
CentOS = "centos" | ||
|
||
// Fedora is done | ||
Fedora = "fedora" | ||
|
||
// Amazon is done | ||
Amazon = "amazon" | ||
|
||
// Oracle is done | ||
Oracle = "oracle" | ||
|
||
// FreeBSD currently doesn't support docker | ||
// FreeBSD = "freebsd" | ||
|
||
// Windows only run windows os | ||
// TODO : support windows | ||
Windows = "windows" | ||
|
||
// OpenSUSE is done | ||
OpenSUSE = "opensuse" | ||
|
||
// OpenSUSELeap is | ||
OpenSUSELeap = "opensuse.leap" | ||
|
||
// OpenSUSETumbleweed is done | ||
OpenSUSETumbleweed = "opensuse.tumbleweed" | ||
|
||
// Alpine is done | ||
Alpine = "alpine" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package debian | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"errors" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/knqyf263/fanal/analyzer/os" | ||
|
||
"github.com/knqyf263/fanal/analyzer" | ||
"github.com/knqyf263/fanal/extractor" | ||
) | ||
|
||
func init() { | ||
analyzer.RegisterOSAnalyzer(&debianOSAnalyzer{}) | ||
} | ||
|
||
type debianOSAnalyzer struct{} | ||
|
||
func (a debianOSAnalyzer) Analyze(fileMap extractor.FileMap) (analyzer.OS, error) { | ||
for _, filename := range a.RequiredFiles() { | ||
file, ok := fileMap[filename] | ||
if !ok { | ||
continue | ||
} | ||
scanner := bufio.NewScanner(bytes.NewBuffer(file)) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
|
||
// Ubuntu also exist debian_version, but format is not number | ||
re := regexp.MustCompile(`(\d+).(\d+)`) | ||
if re.MatchString(strings.TrimSpace(line)) { | ||
return analyzer.OS{Family: os.Debian, Name: line}, nil | ||
} | ||
} | ||
} | ||
return analyzer.OS{}, errors.New("debian: Not match") | ||
} | ||
|
||
func (a debianOSAnalyzer) RequiredFiles() []string { | ||
return []string{"etc/debian_version"} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package opensuse | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"errors" | ||
"strings" | ||
|
||
"github.com/knqyf263/fanal/analyzer/os" | ||
|
||
"github.com/knqyf263/fanal/analyzer" | ||
"github.com/knqyf263/fanal/extractor" | ||
) | ||
|
||
func init() { | ||
analyzer.RegisterOSAnalyzer(&opensuseOSAnalyzer{}) | ||
} | ||
|
||
type opensuseOSAnalyzer struct{} | ||
|
||
// TODO : opensuse/tumbleweed, opensuse/leap のimageのときうまくいかないので調査 | ||
// そもそも os-release のファイル自体が認識されていない | ||
func (a opensuseOSAnalyzer) Analyze(fileMap extractor.FileMap) (analyzer.OS, error) { | ||
for _, filename := range a.RequiredFiles() { | ||
file, ok := fileMap[filename] | ||
if !ok { | ||
continue | ||
} | ||
suseName := "" | ||
scanner := bufio.NewScanner(bytes.NewBuffer(file)) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if strings.HasPrefix(line, "NAME=\"openSUSE") { | ||
if strings.Contains(line, "Leap") { | ||
suseName = os.OpenSUSELeap | ||
} else if strings.Contains(line, "Tumbleweed") { | ||
suseName = os.OpenSUSETumbleweed | ||
} else { | ||
suseName = os.OpenSUSE | ||
} | ||
continue | ||
} | ||
|
||
if suseName != "" && strings.HasPrefix(line, "VERSION_ID=") { | ||
return analyzer.OS{ | ||
Family: suseName, | ||
Name: strings.TrimSpace(line[12 : len(line)-1]), | ||
}, nil | ||
} | ||
} | ||
} | ||
return analyzer.OS{}, errors.New("opensuse: Not match") | ||
} | ||
|
||
func (a opensuseOSAnalyzer) RequiredFiles() []string { | ||
return []string{"etc/os-release"} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package redhatbase | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"errors" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/knqyf263/fanal/analyzer/os" | ||
|
||
"github.com/knqyf263/fanal/analyzer" | ||
"github.com/knqyf263/fanal/extractor" | ||
) | ||
|
||
func init() { | ||
analyzer.RegisterOSAnalyzer(&redhatOSAnalyzer{}) | ||
} | ||
|
||
type redhatOSAnalyzer struct{} | ||
|
||
var redhatRe = regexp.MustCompile(`(.*) release (\d[\d\.]*)`) | ||
|
||
func (a redhatOSAnalyzer) Analyze(fileMap extractor.FileMap) (analyzer.OS, error) { | ||
if file, ok := fileMap["etc/centos-release"]; ok { | ||
scanner := bufio.NewScanner(bytes.NewBuffer(file)) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
result := redhatRe.FindStringSubmatch(strings.TrimSpace(line)) | ||
if len(result) != 3 { | ||
return analyzer.OS{}, errors.New("cent: Invalid centos-release") | ||
} | ||
|
||
switch strings.ToLower(result[1]) { | ||
case "centos", "centos linux": | ||
return analyzer.OS{Family: os.CentOS, Name: result[2]}, nil | ||
} | ||
} | ||
} | ||
|
||
if file, ok := fileMap["etc/oracle-release"]; ok { | ||
scanner := bufio.NewScanner(bytes.NewBuffer(file)) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
result := redhatRe.FindStringSubmatch(strings.TrimSpace(line)) | ||
if len(result) != 3 { | ||
return analyzer.OS{}, errors.New("oracle: Invalid oracle-release") | ||
} | ||
return analyzer.OS{Family: os.Oracle, Name: result[2]}, nil | ||
} | ||
} | ||
|
||
if file, ok := fileMap["etc/fedora-release"]; ok { | ||
scanner := bufio.NewScanner(bytes.NewBuffer(file)) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
result := redhatRe.FindStringSubmatch(strings.TrimSpace(line)) | ||
if len(result) != 3 { | ||
return analyzer.OS{}, errors.New("cent: Invalid fedora-release") | ||
} | ||
|
||
switch strings.ToLower(result[1]) { | ||
case "fedora", "fedora linux": | ||
return analyzer.OS{Family: os.Fedora, Name: result[2]}, nil | ||
} | ||
} | ||
} | ||
|
||
if file, ok := fileMap["etc/redhat-release"]; ok { | ||
scanner := bufio.NewScanner(bytes.NewBuffer(file)) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
result := redhatRe.FindStringSubmatch(strings.TrimSpace(line)) | ||
if len(result) != 3 { | ||
return analyzer.OS{}, errors.New("redhat: Invalid redhat-release") | ||
} | ||
|
||
switch strings.ToLower(result[1]) { | ||
case "centos", "centos linux": | ||
return analyzer.OS{Family: os.CentOS, Name: result[2]}, nil | ||
case "oracle", "oracle linux", "oracle linux server": | ||
return analyzer.OS{Family: os.Oracle, Name: result[2]}, nil | ||
case "fedora", "fedora linux": | ||
return analyzer.OS{Family: os.Fedora, Name: result[2]}, nil | ||
default: | ||
return analyzer.OS{Family: os.RedHat, Name: result[2]}, nil | ||
} | ||
} | ||
} | ||
|
||
return analyzer.OS{}, errors.New("redhatbase : Not match") | ||
} | ||
|
||
func (a redhatOSAnalyzer) RequiredFiles() []string { | ||
return []string{ | ||
"etc/redhat-release", | ||
"etc/oracle-release", | ||
"etc/fedora-release", | ||
"etc/centos-release", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package ubuntu | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"errors" | ||
"strings" | ||
|
||
"github.com/knqyf263/fanal/analyzer/os" | ||
|
||
"github.com/knqyf263/fanal/analyzer" | ||
"github.com/knqyf263/fanal/extractor" | ||
) | ||
|
||
func init() { | ||
analyzer.RegisterOSAnalyzer(&ubuntuOSAnalyzer{}) | ||
} | ||
|
||
type ubuntuOSAnalyzer struct{} | ||
|
||
func (a ubuntuOSAnalyzer) Analyze(fileMap extractor.FileMap) (analyzer.OS, error) { | ||
for _, filename := range a.RequiredFiles() { | ||
file, ok := fileMap[filename] | ||
if !ok { | ||
continue | ||
} | ||
isUbuntu := false | ||
scanner := bufio.NewScanner(bytes.NewBuffer(file)) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if line == "DISTRIB_ID=Ubuntu" { | ||
isUbuntu = true | ||
continue | ||
} | ||
|
||
if isUbuntu && strings.HasPrefix(line, "DISTRIB_RELEASE=") { | ||
return analyzer.OS{ | ||
Family: os.Ubuntu, | ||
Name: strings.TrimSpace(line[16:]), | ||
}, nil | ||
} | ||
} | ||
} | ||
return analyzer.OS{}, errors.New("ubuntu: Not match") | ||
} | ||
|
||
func (a ubuntuOSAnalyzer) RequiredFiles() []string { | ||
return []string{"etc/lsb-release"} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters