forked from owasp-amass/amass
/
datasrcs.go
121 lines (103 loc) · 3.02 KB
/
datasrcs.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
// Copyright © by Jeff Foley 2017-2022. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// SPDX-License-Identifier: Apache-2.0
package config
import (
"fmt"
"math/rand"
"strings"
"github.com/caffix/stringset"
"github.com/go-ini/ini"
)
// DataSourceConfig contains the configurations specific to a data source.
type DataSourceConfig struct {
Name string
TTL int `ini:"ttl"`
creds map[string]*Credentials
}
// Credentials contains values required for authenticating with web APIs.
type Credentials struct {
Name string
Username string `ini:"username"`
Password string `ini:"password"`
Key string `ini:"apikey"`
Secret string `ini:"secret"`
}
// GetDataSourceConfig returns the DataSourceConfig associated with the data source name argument.
func (c *Config) GetDataSourceConfig(source string) *DataSourceConfig {
c.Lock()
defer c.Unlock()
key := strings.ToLower(strings.TrimSpace(source))
if key == "" {
return nil
}
if c.datasrcConfigs == nil {
c.datasrcConfigs = make(map[string]*DataSourceConfig)
}
if _, found := c.datasrcConfigs[key]; !found {
c.datasrcConfigs[key] = &DataSourceConfig{Name: key}
}
return c.datasrcConfigs[key]
}
// AddCredentials adds the Credentials provided to the configuration.
func (dsc *DataSourceConfig) AddCredentials(cred *Credentials) error {
if cred == nil || cred.Name == "" {
return fmt.Errorf("AddCredentials: The Credentials argument is invalid")
}
if dsc.creds == nil {
dsc.creds = make(map[string]*Credentials)
}
dsc.creds[cred.Name] = cred
return nil
}
// GetCredentials returns randomly selected Credentials associated with the receiver configuration.
func (dsc *DataSourceConfig) GetCredentials() *Credentials {
if num := len(dsc.creds); num > 0 {
var creds []*Credentials
for _, c := range dsc.creds {
creds = append(creds, c)
}
return creds[rand.Intn(num)]
}
return nil
}
func (c *Config) loadDataSourceSettings(cfg *ini.File) error {
sec, err := cfg.GetSection("data_sources")
if err != nil {
return err
}
if sec.HasKey("minimum_ttl") {
if ttl, err := sec.Key("minimum_ttl").Int(); err == nil {
c.MinimumTTL = ttl
}
}
for _, child := range sec.ChildSections() {
name := strings.Split(child.Name(), ".")[1]
if name == "disabled" {
// Load up all the disabled data source names
c.SourceFilter.Sources = stringset.Deduplicate(child.Key("data_source").ValueWithShadows())
c.SourceFilter.Include = false
continue
}
dsc := c.GetDataSourceConfig(name)
// Parse the Database information and assign to the Config
if err := child.MapTo(dsc); err != nil {
continue
}
if c.MinimumTTL > dsc.TTL {
dsc.TTL = c.MinimumTTL
}
// Check for data source credentials
for _, cr := range child.ChildSections() {
setName := strings.Split(cr.Name(), ".")[2]
creds := &Credentials{Name: setName}
if err := cr.MapTo(creds); err != nil {
return err
}
if err := dsc.AddCredentials(creds); err != nil {
return err
}
}
}
return nil
}