Exemple #1
0
func (gc *GlobalConfig) Parse(data map[interface{}]interface{}, config *entity.Config) (err error) {

	if config == nil {
		config = entity.NewConfig()
	}

	//Parse default section.
	defaultSection, ok := data["default"].(map[interface{}]interface{})
	if ok {
		config.DefaultCredential.User, ok = defaultSection["user"].(string)
		config.DefaultCredential.Pass, ok = defaultSection["pass"].(string)
		config.DefaultCredential.Identity, ok = defaultSection["identity"].(string)
	}

	//Parse host section.
	hostSection, ok := data["host"].(map[interface{}]interface{})
	if ok {
		var tmpCred *entity.Credential
		for hostName, _ := range hostSection {
			hostEntry, ok := hostSection[hostName.(string)].(map[interface{}]interface{})
			if !ok {
				err = errors.New("Invalid host section entry. host name: " + hostName.(string))
				return
			}
			tmpCred = entity.NewCredential()
			tmpCred.User, ok = hostEntry["user"].(string)
			tmpCred.Pass, ok = hostEntry["pass"].(string)
			tmpCred.Identity, ok = hostEntry["identity"].(string)

			config.Hosts[hostName.(string)] = tmpCred
		}
	}
	return
}
Exemple #2
0
func TestGetDefaultCrediential(t *testing.T) {

	config := entity.NewConfig()

	defaultCred := entity.NewCredential()
	defaultCred.User = "******"

	hostACred := entity.NewCredential()
	hostACred.User = "******"

	hostBCred := entity.NewCredential()
	hostBCred.User = "******"

	config.DefaultCredential = defaultCred
	config.Hosts["host-a"] = hostACred
	config.Hosts["host-b"] = hostBCred

	result := config.GetDefaultCredential("host-a")
	if result.User != "host-a-user" {
		t.Log("host-a user not matched.")
		t.Fail()
	}

	result = config.GetDefaultCredential("host-b")
	if result.User != "host-b-user" {
		t.Log("host-b user not matched.")
		t.Fail()
	}

	result = config.GetDefaultCredential("host-c")
	if result.User != "default-user" {
		t.Log("host-c user not matched.")
		t.Fail()
	}

	result = config.GetDefaultCredential("")
	if result.User != "default-user" {
		t.Log("empty host user not matched.")
		t.Fail()
	}
}
Exemple #3
0
// Returns new extssh.InputEntry.
func NewInputEntry() *InputEntry {
	i := new(InputEntry)
	i.InputEntryBase = new(entity.InputEntryBase)
	i.Cred = entity.NewCredential()
	return i
}
func TestCredentialOmitted(t *testing.T) {

	yamlString := []byte(`input:
  logA:
    type: ssh
    host:
      - 1.1.1.1
      - 2.2.2.2
    command: abc
  logB:
    type: ssh
    host: 3.3.3.3
    command: abc
`)

	var yamlData interface{}
	err := yaml.Unmarshal(yamlString, &yamlData)
	if err != nil {
		t.Log("Failed to parse yaml.")
		t.Fail()
		return
	}

	inputSection := yamlData.(map[interface{}]interface{})["input"].(map[interface{}]interface{})

	inputEntryParser := extssh.NewInputEntryParser()

	var entries []entity.InputEntry
	var tempEntries []entity.InputEntry
	config := entity.NewConfig()
	config.DefaultCredential.User = "******"
	config.DefaultCredential.Pass = "******"
	config.Hosts["3.3.3.3"] = entity.NewCredential()
	config.Hosts["3.3.3.3"].User = "******"
	config.Hosts["3.3.3.3"].Pass = "******"

	for name, inputEntry := range inputSection {
		input := inputEntry.(map[interface{}]interface{})

		tempEntries, err = inputEntryParser.CreateInputEntriesFromData(config, name.(string), input)
		entries = append(entries, tempEntries...)
	}

	if len(entries) != 3 {
		t.Logf("Config entry count does not matched. expected: 3, actual: %d", len(entries))
		t.Fail()
		return
	}

	expects := []map[string]string{
		{"host": "1.1.1.1", "name": "logA", "user": "******"},
		{"host": "2.2.2.2", "name": "logA", "user": "******"},
		{"host": "3.3.3.3", "name": "logB", "user": "******"},
	}

	for i, expected := range expects {
		entry := entries[i].(*extssh.InputEntry)

		if entry.Host != expected["host"] {
			t.Logf("Host is not expected. index=%d: value=%s", i, entry.Host)
			t.Fail()
		}
		if entry.Name != expected["name"] {
			t.Logf("Log name is not expected. index=%d: value=%s", i, entry.Name)
			t.Fail()
		}
		if entry.Cred.User != expected["user"] {
			t.Logf("user is not expected. index=%d: value=%s", i, entry.Cred.User)
			t.Fail()
		}
	}

}