Class: Amarillo::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/amarillo/environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amarilloHome: DefaultAmarilloHome) ⇒ Environment

Returns a new instance of Environment.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/amarillo/environment.rb', line 36

def initialize(amarilloHome:  DefaultAmarilloHome)

  @logger = Logger.new(STDOUT)
  @logger.level = Logger::INFO

  @amarilloHome    = amarilloHome
  @certificatePath = amarilloHome + "/certificates"
  @keyPath         = amarilloHome + "/keys"
  @configPath      = amarilloHome
  @configsPath     = amarilloHome + "/configs"
  @configFile      = amarilloHome + "/config.yml"
  @awsEnvFile      = amarilloHome + "/aws.env"

end

Instance Attribute Details

#awsEnvFileObject (readonly)

Returns the value of attribute awsEnvFile.



34
35
36
# File 'lib/amarillo/environment.rb', line 34

def awsEnvFile
  @awsEnvFile
end

#certificatePathObject (readonly)

Returns the value of attribute certificatePath.



34
35
36
# File 'lib/amarillo/environment.rb', line 34

def certificatePath
  @certificatePath
end

#configObject (readonly)

Returns the value of attribute config.



34
35
36
# File 'lib/amarillo/environment.rb', line 34

def config
  @config
end

#configPathObject (readonly)

Returns the value of attribute configPath.



34
35
36
# File 'lib/amarillo/environment.rb', line 34

def configPath
  @configPath
end

#configsPathObject (readonly)

Returns the value of attribute configsPath.



34
35
36
# File 'lib/amarillo/environment.rb', line 34

def configsPath
  @configsPath
end

#keyPathObject (readonly)

Returns the value of attribute keyPath.



34
35
36
# File 'lib/amarillo/environment.rb', line 34

def keyPath
  @keyPath
end

Instance Method Details

#get_zone_nameserversObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/amarillo/environment.rb', line 205

def get_zone_nameservers

  self.load_config

  nameservers      = @config["defaults"]["nameservers"]
  zone             = @config["defaults"]["zone"]

  @logger.info "Looking up nameservers for #{zone}"

  zone_nameservers = []
  Resolv::DNS.open(nameserver:  nameservers) do |dns|
    while zone_nameservers.length == 0
      zone_nameservers = dns.getresources(
        zone,
        Resolv::DNS::Resource::IN::NS
        ).map(&:name).map(&:to_s)
    end
  end

  @logger.info "Found #{zone_nameservers.length} nameservers for zone #{zone}:  #{zone_nameservers}"

  return zone_nameservers
end

#init(zone = nil, email = nil) ⇒ Object

Public method to create default configuration files



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
# File 'lib/amarillo/environment.rb', line 52

def init(zone = nil, email = nil)

  unless File.exist?(@configsPath) and File.directory?(@configsPath)
    begin
      @logger.info "Creating #{@configsPath} directory"
      FileUtils.mkpath(@configsPath)
    rescue
      @logger.error("Cannot create #{@configsPath} directory")
      return false
    end
  end

  unless File.exist?(@certificatePath) and File.directory?(@certificatePath)
    begin
      @logger.info "Creating #{@certificatePath} directory"
      FileUtils.mkpath(@certificatePath)
    rescue
      @logger.error("Cannot create #{@certificatePath} directory")
      return false
    end 
  end 

  unless File.exist?(@keyPath) and File.directory?(@keyPath)
    begin
      @logger.info "Creating #{@keyPath} directory"
      FileUtils.mkpath(@keyPath)
    rescue
      @logger.error("Cannot create #{@keyPath} directory")
      return false
    end 
  end

  # Create aws.env
  unless File.exist?(@awsEnvFile) then
    awsEnv = <<-HEREDOC
[default]
aws_access_key_id = 
aws_secret_access_key = 
HEREDOC
    @logger.info("Creating blank #{@awsEnvFile}")
    @logger.warn("NOTE:  aws_access_key_id and aws_secret_access_key must be specified in this file.")
    File.write(@awsEnvFile, awsEnv)
  else
    @logger.info("Refusing to overwrite #{@awsEnvFile}")
  end

  # Create config.yml
  unless File.exist?(@configFile) then
    @logger.info("Creating default configuration #{@configFile}")
    config = {
      "defaults" => {
        "region"      =>  'us-east-2',
        "profile"     => 'default',
        "email"       =>  email,
        "zone"        =>  zone,
        "nameservers" =>  ['208.67.222.222', '9.9.9.9'],
        "key_type"    =>  'ec,secp384r1',
        "owner"       =>  'root',
        "group"       =>  'root',
        "key_mode"    =>  0660
    }}
    File.write(@configFile, config.to_yaml)
  else
    @logger.info("Refusing to overwrite #{@configFile}")
  end

  # Create RSA private key for Let's Encrypt account
  privateKeyPath = "#{@keyPath}/letsencrypt.key"

  unless File.exist? privateKeyPath then
    @logger.info "Generating 4096-bit RSA private key for Let's Encrypt account"

    privateKey = OpenSSL::PKey::RSA.new(4096)


    File.open(privateKeyPath, "w") do |f|
      f.puts privateKey.to_pem.to_s
    end
    File.chmod(0400, privateKeyPath)
  end
end

#load_configObject



199
200
201
202
203
# File 'lib/amarillo/environment.rb', line 199

def load_config()
  if verify_config() then
    @config = YAML.load(File.read(@configFile))
  end
end

#verifyObject

Verify paths exist and are writable Verify aws.env exists and is formatted correctly Verify config.yml exists and is formatted correctly



139
140
141
142
143
144
145
# File 'lib/amarillo/environment.rb', line 139

def verify
  @logger.info "Verifying amarillo environment"
  if not verify_env()    then return false end
  if not verify_awsenv() then return false end
  if not verify_config() then return false end
  return true
end

#verify_awsenvObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/amarillo/environment.rb', line 161

def verify_awsenv()
  awsEnvFile = Pathname.new(@awsEnvFile)
  if not awsEnvFile.exist? then 
    @logger.error("#{awsEnvFile} does not exist")
    return false 
  end           

  awsCredentials = Aws::SharedCredentials.new(path: "#{@awsEnvFile}")

  if awsCredentials.credentials.access_key_id.length != 20 then
    @logger.error("#{@awsEnvFile} aws_access_key_id does not appear to be valid")
    return false
  end

  if awsCredentials.credentials.secret_access_key.length != 40 then
    @logger.error("#{@awsEnvFile} aws_secret_access_key does not appear to be valid")
    return false
  end

  return true
end

#verify_configObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/amarillo/environment.rb', line 183

def verify_config()
  if not File.exist?(@configFile) then
    @logger.error("#{@configFile} does not exist")
    return false
  end

  begin
    YAML.load(File.read(@configFile))
  rescue
    @logger.error("Unable to load configuration file")
    return false
  end 

  return true
end

#verify_envObject



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/amarillo/environment.rb', line 147

def verify_env
  unless File.stat(@certificatePath).writable? then
    @logger.error(@certificatePath + " is not writable")
    return false
  end

  unless File.stat(@keyPath).writable? then
    @logger.error(@keyPath + " is not writable")
    return false
  end 

  return true
end