Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 0.2.0 (2017-09-21)

Renamed `Identity` to `LoginGov` because `Identity` because of a name collision with the `identities` table ActiveRecord model in `identity-idp`

- Added `LoginGov::Hostdata.domain`
- Added `LoginGov::Hostdata.env`

# 0.1.0 (2017-09-21)

- Added `Identity::Hostdata.domain`
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Identity::Hostdata
# LoginGov::Hostdata (`identity-hostdata`)

A gem to help read configuration from login.gov infrastructure

Expand All @@ -17,7 +17,7 @@ Use this gem to access config data on a per-host basis
```
require 'identity/hostdata'

Identity::Hostdata.domain
LoginGov::Hostdata.domain
# => "login.gov"
```

Expand Down
4 changes: 2 additions & 2 deletions identity-hostdata.gemspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# coding: utf-8
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "identity/hostdata/version"
require "login_gov/hostdata/version"

Gem::Specification.new do |spec|
spec.name = "identity-hostdata"
spec.version = Identity::Hostdata::VERSION
spec.version = LoginGov::Hostdata::VERSION
spec.authors = ["Zach Margolis"]
spec.email = ["zachary.margolis@gsa.gov"]

Expand Down
40 changes: 1 addition & 39 deletions lib/identity/hostdata.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1 @@
require "identity/hostdata/version"

module Identity
module Hostdata
class MissingConfigError < StandardError; end

CONFIG_DIR = '/etc/login.gov'
DOMAIN_PATH = File.join(CONFIG_DIR, 'info/domain')
ENV_PATH = File.join(CONFIG_DIR, 'info/env')

def self.domain
@domain ||= begin
File.read(DOMAIN_PATH).chomp
rescue Errno::ENOENT => err
raise MissingConfigError, err.message if in_datacenter?
end
end

def self.env
@env ||= begin
File.read(ENV_PATH).chomp
rescue Errno::ENOENT => err
raise MissingConfigError, err.message if in_datacenter?
end
end

def self.in_datacenter?
File.directory?(CONFIG_DIR)
end

# @api private
# Used to clear memoized values (intended for specs)
def self.reset!
instance_variables.each do |variable|
remove_instance_variable(variable)
end
end
end
end
require "login_gov/hostdata"
5 changes: 0 additions & 5 deletions lib/identity/hostdata/version.rb

This file was deleted.

39 changes: 39 additions & 0 deletions lib/login_gov/hostdata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require "login_gov/hostdata/version"

module LoginGov
module Hostdata
class MissingConfigError < StandardError; end

CONFIG_DIR = '/etc/login.gov'
DOMAIN_PATH = File.join(CONFIG_DIR, 'info/domain')
ENV_PATH = File.join(CONFIG_DIR, 'info/env')

def self.domain
@domain ||= begin
File.read(DOMAIN_PATH).chomp
rescue Errno::ENOENT => err
raise MissingConfigError, err.message if in_datacenter?
end
end

def self.env
@env ||= begin
File.read(ENV_PATH).chomp
rescue Errno::ENOENT => err
raise MissingConfigError, err.message if in_datacenter?
end
end

def self.in_datacenter?
File.directory?(CONFIG_DIR)
end

# @api private
# Used to clear memoized values (intended for specs)
def self.reset!
instance_variables.each do |variable|
remove_instance_variable(variable)
end
end
end
end
5 changes: 5 additions & 0 deletions lib/login_gov/hostdata/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module LoginGov
module Hostdata
VERSION = "0.2.0"
end
end
26 changes: 13 additions & 13 deletions spec/identity/hostdata_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require "spec_helper"

RSpec.describe Identity::Hostdata do
RSpec.describe LoginGov::Hostdata do
it "has a version number" do
expect(Identity::Hostdata::VERSION).not_to be nil
expect(LoginGov::Hostdata::VERSION).not_to be nil
end

context 'reading config from the filesystem' do
around(:each) do |ex|
Identity::Hostdata.reset!
LoginGov::Hostdata.reset!

FakeFS.with_fresh do
ex.run
Expand All @@ -25,21 +25,21 @@
end

it 'reads the contents of the file' do
expect(Identity::Hostdata.domain).to eq('identitysandbox.gov')
expect(LoginGov::Hostdata.domain).to eq('identitysandbox.gov')
end
end

context 'when the info/domain file does not exist' do
it 'blows up' do
expect { Identity::Hostdata.domain }.
to raise_error(Identity::Hostdata::MissingConfigError)
expect { LoginGov::Hostdata.domain }.
to raise_error(LoginGov::Hostdata::MissingConfigError)
end
end
end

context 'when /etc/login.gov does not exist (development environment)' do
it 'is nil' do
expect(Identity::Hostdata.domain).to eq(nil)
expect(LoginGov::Hostdata.domain).to eq(nil)
end
end
end
Expand All @@ -55,21 +55,21 @@
end

it 'reads the contents of the file' do
expect(Identity::Hostdata.env).to eq('staging')
expect(LoginGov::Hostdata.env).to eq('staging')
end
end

context 'when the info/env file does not exist' do
it 'blows up' do
expect { Identity::Hostdata.env }.
to raise_error(Identity::Hostdata::MissingConfigError)
expect { LoginGov::Hostdata.env }.
to raise_error(LoginGov::Hostdata::MissingConfigError)
end
end
end

context 'when /etc/login.gov does not exist (development environment)' do
it 'is nil' do
expect(Identity::Hostdata.env).to eq(nil)
expect(LoginGov::Hostdata.env).to eq(nil)
end
end
end
Expand All @@ -78,11 +78,11 @@
it 'is true when the /etc/login.gov directory exists' do
FileUtils.mkdir_p('/etc/login.gov')

expect(Identity::Hostdata.in_datacenter?).to eq(true)
expect(LoginGov::Hostdata.in_datacenter?).to eq(true)
end

it 'is false when the /etc/login.gov does note exist' do
expect(Identity::Hostdata.in_datacenter?).to eq(false)
expect(LoginGov::Hostdata.in_datacenter?).to eq(false)
end
end
end
Expand Down