This repository has been archived by the owner on Jun 12, 2021. It is now read-only.
/
ansible_ssh_settings.go
167 lines (151 loc) · 6.13 KB
/
ansible_ssh_settings.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package types
import (
"os"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
)
// AnsibleSSHSettings represents Ansible process SSH settings.
type AnsibleSSHSettings struct {
connectTimeoutSeconds int
connectAttempts int
sshKeyscanSeconds int
insecureNoStrictHostKeyChecking bool
insecureBastionNoStrictHostKeyChecking bool
userKnownHostsFile string
bastionUserKnownHostsFile string
overrideStrictHostKeyChecking bool
}
const (
// default values:
ansibleSSHDefaultConnectTimeoutSeconds = 10
ansibleSSHDefaultConnectAttempts = 10
ansibleSSHDefaultSSHKeyscanSeconds = 60
// attribute names:
ansibleSSHAttributeConnectTimeoutSeconds = "connect_timeout_seconds"
ansibleSSHAttributeConnectAttempts = "connection_attempts"
ansibleSSHAttributeSSHKeyscanSeconds = "ssh_keyscan_timeout"
ansibleSSHAttributeInsecureNoStrictHostKeyChecking = "insecure_no_strict_host_key_checking"
ansibleSSHAttributeInsecureBastionNoStrictHostKeyChecking = "insecure_bastion_no_strict_host_key_checking"
ansibleSSHAttributeUserKnownHostsFile = "user_known_hosts_file"
ansibleSSHAttributeBastionUserKnownHostsFile = "bastion_user_known_hosts_file"
// environment variable names:
ansibleSSHEnvConnectTimeoutSeconds = "TF_PROVISIONER_ANSIBLE_SSH_CONNECT_TIMEOUT_SECONDS"
ansibleSSHEnvConnectAttempts = "TF_PROVISIONER_ANSIBLE_SSH_CONNECTION_ATTEMPTS"
ansibleSSHEnvSSHKeyscanSeconds = "TF_PROVISIONER_SSH_KEYSCAN_TIMEOUT_SECONDS"
)
// NewAnsibleSSHSettingsSchema returns a new AnsibleSSHSettings schema.
func NewAnsibleSSHSettingsSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
ansibleSSHAttributeConnectTimeoutSeconds: &schema.Schema{
Type: schema.TypeInt,
Optional: true,
DefaultFunc: func() (interface{}, error) {
if val, err := strconv.Atoi(os.Getenv(ansibleSSHEnvConnectTimeoutSeconds)); err == nil {
return val, nil
}
return ansibleSSHDefaultConnectTimeoutSeconds, nil
},
},
ansibleSSHAttributeConnectAttempts: &schema.Schema{
Type: schema.TypeInt,
Optional: true,
DefaultFunc: func() (interface{}, error) {
if val, err := strconv.Atoi(os.Getenv(ansibleSSHEnvConnectAttempts)); err == nil {
return val, nil
}
return ansibleSSHDefaultConnectAttempts, nil
},
},
ansibleSSHAttributeSSHKeyscanSeconds: &schema.Schema{
Type: schema.TypeInt,
Optional: true,
DefaultFunc: func() (interface{}, error) {
if val, err := strconv.Atoi(os.Getenv(ansibleSSHEnvSSHKeyscanSeconds)); err == nil {
return val, nil
}
return ansibleSSHDefaultSSHKeyscanSeconds, nil
},
},
ansibleSSHAttributeInsecureNoStrictHostKeyChecking: &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},
ansibleSSHAttributeInsecureBastionNoStrictHostKeyChecking: &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},
ansibleSSHAttributeUserKnownHostsFile: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
},
ansibleSSHAttributeBastionUserKnownHostsFile: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
},
},
},
}
}
// NewAnsibleSSHSettingsFromInterface reads AnsibleSSHSettings configuration from Terraform schema.
func NewAnsibleSSHSettingsFromInterface(i interface{}, ok bool) *AnsibleSSHSettings {
v := &AnsibleSSHSettings{
connectTimeoutSeconds: ansibleSSHDefaultConnectTimeoutSeconds,
connectAttempts: ansibleSSHDefaultConnectAttempts,
sshKeyscanSeconds: ansibleSSHDefaultSSHKeyscanSeconds,
}
if ok {
vals := mapFromTypeSetList(i.(*schema.Set).List())
v.connectTimeoutSeconds = vals[ansibleSSHAttributeConnectTimeoutSeconds].(int)
v.connectAttempts = vals[ansibleSSHAttributeConnectAttempts].(int)
v.sshKeyscanSeconds = vals[ansibleSSHAttributeSSHKeyscanSeconds].(int)
v.insecureNoStrictHostKeyChecking = vals[ansibleSSHAttributeInsecureNoStrictHostKeyChecking].(bool)
v.insecureBastionNoStrictHostKeyChecking = vals[ansibleSSHAttributeInsecureBastionNoStrictHostKeyChecking].(bool)
v.userKnownHostsFile = vals[ansibleSSHAttributeUserKnownHostsFile].(string)
v.bastionUserKnownHostsFile = vals[ansibleSSHAttributeBastionUserKnownHostsFile].(string)
}
return v
}
// ConnectTimeoutSeconds reutrn Ansible process SSH connection timeout.
func (v *AnsibleSSHSettings) ConnectTimeoutSeconds() int {
return v.connectTimeoutSeconds
}
// ConnectAttempts reutrn Ansible process SSH connection attempt count.
func (v *AnsibleSSHSettings) ConnectAttempts() int {
return v.connectAttempts
}
// SSHKeyscanSeconds reutrn Ansible process SSH keyscan timeout.
func (v *AnsibleSSHSettings) SSHKeyscanSeconds() int {
return v.sshKeyscanSeconds
}
// InsecureNoStrictHostKeyChecking if true, SSH to the target host uses -o StrictHostKeyChecking=no.
func (v *AnsibleSSHSettings) InsecureNoStrictHostKeyChecking() bool {
if v.overrideStrictHostKeyChecking || v.insecureNoStrictHostKeyChecking {
return true
} else {
return false
}
}
// SetOverrideStrictHostKeyChecking is used by the provisioner when attached to a null_resource
func (v *AnsibleSSHSettings) SetOverrideStrictHostKeyChecking() {
v.overrideStrictHostKeyChecking = true
}
// InsecureBastionNoStrictHostKeyChecking if true, SSH to the bastion host uses -o StrictHostKeyChecking=no.
func (v *AnsibleSSHSettings) InsecureBastionNoStrictHostKeyChecking() bool {
return v.insecureBastionNoStrictHostKeyChecking
}
// UserKnownHostsFile returns a path to the user known hosts file for the target host.
func (v *AnsibleSSHSettings) UserKnownHostsFile() string {
return v.userKnownHostsFile
}
// BastionUserKnownHostsFile returns a path to the user known hosts file for the bastion host.
func (v *AnsibleSSHSettings) BastionUserKnownHostsFile() string {
return v.bastionUserKnownHostsFile
}