-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6595 from Checkmarx/kics-970
feat(parser): ansible configuration support
- Loading branch information
Showing
12 changed files
with
443 additions
and
8 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package ansibleconfig | ||
|
||
import ( | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/Checkmarx/kics/pkg/model" | ||
"github.com/Checkmarx/kics/pkg/parser/ansible/ini/comments" | ||
"github.com/bigkevmcd/go-configparser" | ||
) | ||
|
||
// Parser defines a parser type | ||
type Parser struct { | ||
} | ||
|
||
func (p *Parser) Resolve(fileContent []byte, filename string) ([]byte, error) { | ||
return fileContent, nil | ||
} | ||
|
||
// Parse parses .cfg/.conf file and returns it as a Document | ||
func (p *Parser) Parse(filePath string, fileContent []byte) ([]model.Document, []int, error) { | ||
model.NewIgnore.Reset() | ||
|
||
reader := strings.NewReader(string(fileContent)) | ||
configparser.Delimiters("=") | ||
inline := configparser.InlineCommentPrefixes([]string{";"}) | ||
|
||
config, err := configparser.ParseReaderWithOptions(reader, inline) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
doc := make(map[string]interface{}) | ||
doc["groups"] = refactorConfig(config) | ||
|
||
ignoreLines := comments.GetIgnoreLines(strings.Split(string(fileContent), "\n")) | ||
|
||
return []model.Document{doc}, ignoreLines, nil | ||
} | ||
|
||
// refactorConfig removes all extra information and tries to convert | ||
func refactorConfig(config *configparser.ConfigParser) (doc *model.Document) { | ||
doc = emptyDocument() | ||
for _, section := range config.Sections() { | ||
dict, err := config.Items(section) | ||
if err != nil { | ||
continue | ||
} | ||
dictRefact := make(map[string]interface{}) | ||
for key, value := range dict { | ||
if boolValue, err := strconv.ParseBool(value); err == nil { | ||
dictRefact[key] = boolValue | ||
} else if floatValue, err := strconv.ParseFloat(value, 64); err == nil { | ||
dictRefact[key] = floatValue | ||
} else if strings.Contains(value, ",") { | ||
re := regexp.MustCompile(`\w+`) | ||
matches := re.FindAllString(value, -1) | ||
if len(matches) > 0 { | ||
dictRefact[key] = matches | ||
} else { | ||
dictRefact[key] = []string{} | ||
} | ||
} else if strings.Contains(value, ":") { | ||
re := regexp.MustCompile(`\w+`) | ||
matches := re.FindAllString(value, -1) | ||
if len(matches) > 0 { | ||
dictRefact[key] = matches | ||
} else { | ||
dictRefact[key] = []string{} | ||
} | ||
} else if value == "[]" { | ||
dictRefact[key] = []string{} | ||
} else { | ||
dictRefact[key] = value | ||
} | ||
} | ||
(*doc)[section] = dictRefact | ||
} | ||
|
||
return doc | ||
} | ||
|
||
// SupportedExtensions returns extensions supported by this parser, which are only ini extension | ||
func (p *Parser) SupportedExtensions() []string { | ||
return []string{".cfg", ".conf"} | ||
} | ||
|
||
// SupportedTypes returns types supported by this parser, which is ansible | ||
func (p *Parser) SupportedTypes() map[string]bool { | ||
return map[string]bool{ | ||
"ansible": true, | ||
} | ||
} | ||
|
||
// GetKind returns CFG constant kind | ||
func (p *Parser) GetKind() model.FileKind { | ||
return model.KindCFG | ||
} | ||
|
||
// GetCommentToken return the comment token of CFG/CONF - # | ||
func (p *Parser) GetCommentToken() string { | ||
return "#" | ||
} | ||
|
||
// GetResolvedFiles returns resolved files | ||
func (p *Parser) GetResolvedFiles() map[string]model.ResolvedFile { | ||
return make(map[string]model.ResolvedFile) | ||
} | ||
|
||
// StringifyContent converts original content into string formatted version | ||
func (p *Parser) StringifyContent(content []byte) (string, error) { | ||
return string(content), nil | ||
} | ||
|
||
func emptyDocument() *model.Document { | ||
return &model.Document{} | ||
} |
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,92 @@ | ||
package ansibleconfig | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/Checkmarx/kics/pkg/model" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestParser_GetKind tests the functions [GetKind()] and all the methods called by them | ||
func TestParser_GetKind(t *testing.T) { | ||
p := &Parser{} | ||
require.Equal(t, model.KindCFG, p.GetKind()) | ||
} | ||
|
||
// TestParser_SupportedExtensions tests the functions [SupportedExtensions()] and all the methods called by them | ||
func TestParser_SupportedExtensions(t *testing.T) { | ||
p := &Parser{} | ||
require.Equal(t, []string{".cfg", ".conf"}, p.SupportedExtensions()) | ||
} | ||
|
||
// TestParser_SupportedExtensions tests the functions [SupportedTypes()] and all the methods called by them | ||
func TestParser_SupportedTypes(t *testing.T) { | ||
p := &Parser{} | ||
require.Equal(t, map[string]bool{ | ||
"ansible": true, | ||
}, p.SupportedTypes()) | ||
} | ||
|
||
// TestParser_Parse tests the functions [Parse()] and all the methods called by them | ||
func TestParser_Parse(t *testing.T) { | ||
type args struct { | ||
content []byte | ||
} | ||
tests := []struct { | ||
name string | ||
p *Parser | ||
args args | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "CFG to model.Document", | ||
p: &Parser{}, | ||
args: args{ | ||
content: []byte(` | ||
[defaults] | ||
inventory = /etc/ansible/hosts | ||
library = /usr/share/ansible/ | ||
module_utils = /usr/share/ansible/plugins/modules/ | ||
inventory_plugins = /usr/share/ansible/plugins/inventory/ | ||
roles_path = /etc/ansible/roles | ||
stdout_callback = yaml | ||
forks = 10 | ||
strategy = free | ||
httpapi_plugins=~/.ansible/plugins/httpapi:/usr/share/ansible/plugins/httpapi | ||
internal_poll_interval=0.001 | ||
inventory_plugins=~/.ansible/plugins/inventory:/usr/share/ansible/plugins/inventory | ||
jinja2_extensions=[] | ||
[ssh_connection] | ||
pipelining = True | ||
ssh_args = -o ControlMaster=auto -o ControlPersist=30m -o ControlPath=/tmp/ansible-ssh-%h-%p-%r | ||
[callback_plugins] | ||
profile_tasks = yes | ||
`), | ||
}, | ||
want: "", | ||
wantErr: false, | ||
}, | ||
} | ||
for i, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
p := &Parser{} | ||
switch i { | ||
case 0: | ||
got, _, err := p.Parse("", tt.args.content) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Parser() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
_, err = json.Marshal(got) | ||
if err != nil { | ||
t.Errorf("json.Marshal() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.