Class: Gitlab::QA::Component::LDAP

Inherits:
Object
  • Object
show all
Includes:
Scenario::Actable
Defined in:
lib/gitlab/qa/component/ldap.rb

Constant Summary collapse

LDAP_IMAGE =
'osixia/openldap'.freeze
LDAP_IMAGE_TAG =
'latest'.freeze
LDAP_USER =
'tanuki'.freeze
LDAP_PASSWORD =
'password'.freeze
BOOTSTRAP_LDIF =
'/container/service/slapd/assets/config/bootstrap/ldif/custom'.freeze
FIXTURE_PATH =
File.expand_path('../../../../fixtures/ldap'.freeze, __dir__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Scenario::Actable

#act, included

Constructor Details

#initializeLDAP

Returns a new instance of LDAP.



32
33
34
35
36
37
38
39
# File 'lib/gitlab/qa/component/ldap.rb', line 32

def initialize
  @docker = Docker::Engine.new
  @environment = {}
  @volumes = {}
  @network_aliases = []

  @volumes[FIXTURE_PATH] = BOOTSTRAP_LDIF
end

Instance Attribute Details

#dockerObject (readonly)

Returns the value of attribute docker.



28
29
30
# File 'lib/gitlab/qa/component/ldap.rb', line 28

def docker
  @docker
end

#environmentObject

Returns the value of attribute environment.



29
30
31
# File 'lib/gitlab/qa/component/ldap.rb', line 29

def environment
  @environment
end

#nameObject



65
66
67
# File 'lib/gitlab/qa/component/ldap.rb', line 65

def name
  @name ||= "openldap-#{SecureRandom.hex(4)}"
end

#networkObject

Returns the value of attribute network.



29
30
31
# File 'lib/gitlab/qa/component/ldap.rb', line 29

def network
  @network
end

#volumesObject

Returns the value of attribute volumes.



29
30
31
# File 'lib/gitlab/qa/component/ldap.rb', line 29

def volumes
  @volumes
end

Instance Method Details

#add_network_alias(name) ⇒ Object



61
62
63
# File 'lib/gitlab/qa/component/ldap.rb', line 61

def add_network_alias(name)
  @network_aliases.push(name)
end

#hostnameObject



69
70
71
# File 'lib/gitlab/qa/component/ldap.rb', line 69

def hostname
  "#{name}.#{network}"
end

#instanceObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/gitlab/qa/component/ldap.rb', line 73

def instance
  raise 'Please provide a block!' unless block_given?

  prepare
  start

  yield self
ensure
  teardown
end

#passwordObject



57
58
59
# File 'lib/gitlab/qa/component/ldap.rb', line 57

def password
  LDAP_PASSWORD
end

#prepareObject



84
85
86
87
88
89
90
# File 'lib/gitlab/qa/component/ldap.rb', line 84

def prepare
  @docker.pull(LDAP_IMAGE, LDAP_IMAGE_TAG)

  return if @docker.network_exists?(network)

  @docker.network_create(network)
end

#pullObject



126
127
128
# File 'lib/gitlab/qa/component/ldap.rb', line 126

def pull
  @docker.pull(LDAP_IMAGE, LDAP_IMAGE_TAG)
end

#restartObject



115
116
117
# File 'lib/gitlab/qa/component/ldap.rb', line 115

def restart
  @docker.restart(name)
end

#set_gitlab_credentialsObject



130
131
132
133
# File 'lib/gitlab/qa/component/ldap.rb', line 130

def set_gitlab_credentials
  ::Gitlab::QA::Runtime::Env.ldap_username = username
  ::Gitlab::QA::Runtime::Env.ldap_password = password
end

#startObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/gitlab/qa/component/ldap.rb', line 92

def start
  # copy-service needed for bootstraping LDAP user:
  # https://github.com/osixia/docker-openldap#seed-ldap-database-with-ldif
  docker.run(LDAP_IMAGE, LDAP_IMAGE_TAG, '--copy-service') do |command|
    command << '-d '
    command << "--name #{name}"
    command << "--net #{network}"
    command << "--hostname #{hostname}"

    @volumes.to_h.each do |to, from|
      command.volume(to, from, 'Z')
    end

    @environment.to_h.each do |key, value|
      command.env(key, value)
    end

    @network_aliases.to_a.each do |network_alias|
      command << "--network-alias #{network_alias}"
    end
  end
end

#teardownObject



119
120
121
122
123
124
# File 'lib/gitlab/qa/component/ldap.rb', line 119

def teardown
  raise 'Invalid instance name!' unless name

  @docker.stop(name)
  @docker.remove(name)
end

#tls=(status) ⇒ Object

LDAP_TLS is true by default



42
43
44
45
46
47
48
49
50
51
# File 'lib/gitlab/qa/component/ldap.rb', line 42

def tls=(status)
  if status
    @environment['LDAP_TLS_CRT_FILENAME'] = "#{hostname}.crt"
    @environment['LDAP_TLS_KEY_FILENAME'] = "#{hostname}.key"
    @environment['LDAP_TLS_ENFORCE'] = 'true'
    @environment['LDAP_TLS_VERIFY_CLIENT'] = 'never'
  else
    @environment['LDAP_TLS'] = 'false'
  end
end

#usernameObject



53
54
55
# File 'lib/gitlab/qa/component/ldap.rb', line 53

def username
  LDAP_USER
end