Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(engine): improve ansible detection #6880

Merged
merged 5 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package analyzer

import (
"fmt"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -65,6 +66,7 @@ var (
cicdOnRegex = regexp.MustCompile(`\s*on:\s*`)
cicdJobsRegex = regexp.MustCompile(`\s*jobs:\s*`)
cicdStepsRegex = regexp.MustCompile(`\s*steps:\s*`)
queryRegexPathsAnsible = regexp.MustCompile(fmt.Sprintf(`^.*?%s(?:group|host)_vars%s.*$`, regexp.QuoteMeta(string(os.PathSeparator)), regexp.QuoteMeta(string(os.PathSeparator)))) //nolint:lll
)

var (
Expand Down Expand Up @@ -105,7 +107,7 @@ var (
"hosts", "tasks", "become", "with_items", "with_dict",
"when", "become_pass", "become_exe", "become_flags"}
playBooks = "playbooks"
ansibleHost = "all"
ansibleHost = []string{"all", "ungrouped"}
listKeywordsAnsibleHots = []string{"hosts", "children"}
)

Expand Down Expand Up @@ -552,9 +554,17 @@ func checkYamlPlatform(content []byte, path string) string {
if checkForAnsibleHost(yamlContent) {
return ansible
}
// add for yaml files contained at paths (group_vars, host_vars) related with ansible
if checkForAnsibleByPaths(path) {
return ansible
}
return ""
}

func checkForAnsibleByPaths(path string) bool {
return queryRegexPathsAnsible.MatchString(path)
}

func checkForAnsible(yamlContent model.Document) bool {
isAnsible := false
if play := yamlContent[playBooks]; play != nil {
Expand All @@ -576,11 +586,13 @@ func checkForAnsible(yamlContent model.Document) bool {

func checkForAnsibleHost(yamlContent model.Document) bool {
isAnsible := false
if hosts := yamlContent[ansibleHost]; hosts != nil {
if listHosts, ok := hosts.(map[string]interface{}); ok {
for _, value := range listKeywordsAnsibleHots {
if host := listHosts[value]; host != nil {
isAnsible = true
for _, ansibleDefault := range ansibleHost {
if hosts := yamlContent[ansibleDefault]; hosts != nil {
if listHosts, ok := hosts.(map[string]interface{}); ok {
for _, value := range listKeywordsAnsibleHots {
if host := listHosts[value]; host != nil {
isAnsible = true
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,19 @@ func TestAnalyzer_Analyze(t *testing.T) {
excludeGitIgnore: false,
MaxFileSize: 3,
},
{
name: "analyze_ansible_by_path",
paths: []string{filepath.FromSlash("../../test/fixtures/ansible_project_path")},
wantTypes: []string{"ansible"},
wantExclude: []string{},
typesFromFlag: []string{""},
excludeTypesFromFlag: []string{""},
wantLOC: 54,
wantErr: false,
gitIgnoreFileName: "",
excludeGitIgnore: false,
MaxFileSize: -1,
},
}

for _, tt := range tests {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
# group_vars/db_servers.yml
db_port: 3306
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
# group_vars/web_servers.yml
apache_port: 80
13 changes: 13 additions & 0 deletions test/fixtures/ansible_project_path/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
# playbook.yml
- name: Configure web servers
hosts: web_servers
roles:
- common
- web

- name: Configure database servers
hosts: db_servers
roles:
- common
- db
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
# roles/common/tasks/main.yml
- name: Update packages
package:
name: "*"
state: latest
become: yes
14 changes: 14 additions & 0 deletions test/fixtures/ansible_project_path/roles/db/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
# roles/db/tasks/main.yml
- name: Ensure MySQL is installed
package:
name: mysql-server
state: present
become: yes

- name: Ensure MySQL is running
service:
name: mysql
state: started
enabled: yes
become: yes
14 changes: 14 additions & 0 deletions test/fixtures/ansible_project_path/roles/web/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
# roles/web/tasks/main.yml
- name: Ensure Apache is installed
package:
name: apache2
state: present
become: yes

- name: Ensure Apache is running
service:
name: apache2
state: started
enabled: yes
become: yes
Loading