ldap_tools 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c66ce79c9e09afacf1af7b98f025e5cb352a6504
4
- data.tar.gz: 4c9859ca50e52d1ebae2c6b277a8c233fdc44059
3
+ metadata.gz: 6460a58ed64997c7d6a574bf144f1eb4001bb2b9
4
+ data.tar.gz: 582aedaabe2033a5d36ab53b4e3deff54b443524
5
5
  SHA512:
6
- metadata.gz: 54b284a95ea1803e305b91ff0a17ee7100eb98584e02e5c6d80e477b11b5f4e19643b96b44d8cacfced68db4ce3462cbf3c7c361686cf3860107f8b4a2031565
7
- data.tar.gz: 4ad9fb8fc7dc8d736568e0d5425f18b99cb25dfbdcb9972ca5b6fff21f3dec2bea6d0912506aba08c2e25cf2e2718ec9c7eeafa690878c216325d58b218a97e5
6
+ metadata.gz: e0522e4fc808a6de38d362efc935892bfabd818ff1f85e0f7cdee8852ec4156b863c9d84427ba6881c250174d456eac957b1eaabeb62a8966c1f7fb4ccfaba5a
7
+ data.tar.gz: df556c80555d45698c2de525bad47a135cbb6dea1013088149f68c9c70a9de016a2423f7a250a85060b460c62c9945abb538b3cb9425d2bcc1243a859fcd7052
data/bin/ldaptools CHANGED
@@ -23,9 +23,7 @@ case cmd
23
23
  when "user" # run commands associated with user object
24
24
  Tapjoy::LDAP::User.commands
25
25
  when 'group'
26
- GROUP_SUB_COMMANDS = %w(create delete add_user)
27
- commands('This object is used for group management', cmd, GROUP_SUB_COMMANDS)
28
- Tapjoy::LDAP::Group.new
26
+ Tapjoy::LDAP::Group.commands
29
27
  when 'key'
30
28
  KEY_SUB_COMMANDS = %w(add remove install list show)
31
29
  commands('This object is used for group management', cmd, KEY_SUB_COMMANDS)
@@ -0,0 +1,34 @@
1
+ module Tapjoy
2
+ module LDAP
3
+ module Group
4
+ # Add existing user to existing group
5
+ class AddUser
6
+ def add_user
7
+ puts Tapjoy::LDAP::client.modify(dn, operations)
8
+ end
9
+
10
+ private
11
+ def opts
12
+ @opts ||= Trollop::options do
13
+ # Set help message
14
+ usage 'group add_user [options]'
15
+ synopsis "\nThis command is for adding existing users to existing groups"
16
+
17
+ opt(:group, 'Specify group', :type => :string, :required => true)
18
+ opt(:username, 'Specify username', :type => :string, :required => true)
19
+ end
20
+ end
21
+
22
+ def dn
23
+ @dn ||= "cn=#{opts[:group]},ou=Group,#{Tapjoy::LDAP::client.basedn}"
24
+ end
25
+
26
+ def operations
27
+ # Format is LDAP operation, attribute modified, value modified
28
+ # i.e, add the username to the memberuid attribute for the specified group
29
+ @operations ||= [[:add, :memberUid, opts[:username]]]
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,40 @@
1
+ module Tapjoy
2
+ module LDAP
3
+ module Group
4
+ # Create LDAP group
5
+ class Create
6
+ def create
7
+ # Check for errors
8
+ Trollop::die :type, "argument must be 'user' or 'service'" unless ['user', 'service'].include?opts[:type]
9
+
10
+ puts Tapjoy::LDAP::client.add(dn, ldap_attr)
11
+ end
12
+
13
+ private
14
+
15
+ def opts
16
+ @opts ||= Trollop::options do
17
+ # Set help message
18
+ usage 'group create [options]'
19
+ synopsis "\nThis command is for creating new LDAP groups"
20
+
21
+ opt :name, 'Specify group to create', type: :string, required: true
22
+ opt :type, 'Specfy if this is a user or service group', type: :string, default: 'user'
23
+ end
24
+ end
25
+
26
+ def dn
27
+ @dn ||= "cn=#{opts[:name]},ou=Group,#{Tapjoy::LDAP::client.basedn}"
28
+ end
29
+
30
+ def ldap_attr
31
+ @ldap_attr ||= {
32
+ :cn => opts[:name],
33
+ :objectclass => %w(top posixGroup),
34
+ :gidnumber => Tapjoy::LDAP::client.get_max_id('group', opts[:type])
35
+ }
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,38 @@
1
+ module Tapjoy
2
+ module LDAP
3
+ module Group
4
+ # Delete LDAP group
5
+ class Delete
6
+ def delete
7
+ confirm unless opts[:force]
8
+ puts Tapjoy::LDAP::client.delete(dn)
9
+ end
10
+
11
+ private
12
+ def opts
13
+ @opts ||= Trollop::options do
14
+ # Set help message
15
+ usage 'group delete [options]'
16
+ synopsis "\nThis command is for deleting LDAP groups"
17
+
18
+ opt :name, 'Specify group', type: :string, required: true
19
+ opt :force, 'Force delete'
20
+ end
21
+ end
22
+
23
+ def dn
24
+ @dn ||= "cn=#{opts[:name]},ou=Group,#{Tapjoy::LDAP::client.basedn}"
25
+ end
26
+
27
+ def confirm
28
+ puts "Confirm that you want to delete group #{opts[:group]} (yes/no)"
29
+ print '>'
30
+ confirm = STDIN.gets.chomp().downcase
31
+ unless confirm.eql?('y') || confirm.eql?('yes')
32
+ abort("Deletion of #{ opts[:group] } aborted")
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,7 +1,33 @@
1
+ require_relative 'group/create'
2
+ require_relative 'group/delete'
3
+ require_relative 'group/add_user'
4
+
1
5
  module Tapjoy
2
6
  module LDAP
3
- class Group
7
+ # Entry point for all group subcommands
8
+ module Group
4
9
  class << self
10
+
11
+ SUB_COMMANDS = %w(create delete add_user)
12
+
13
+ def commands
14
+ Trollop::options do
15
+ usage 'group [SUB_COMMAND] [options]'
16
+ synopsis "\nThis object is used for group management\nAvailable subcommands are: #{SUB_COMMANDS}"
17
+
18
+ stop_on SUB_COMMANDS
19
+ end
20
+
21
+ cmd = ARGV.shift
22
+
23
+ case cmd
24
+ when 'create', 'delete', 'add_user'
25
+ send(cmd) # call method with respective name
26
+ else
27
+ raise Tapjoy::LDAP::InvalidArgument
28
+ end
29
+ end
30
+
5
31
  # Lookup GID for the given group
6
32
  def lookup_id(groupname)
7
33
  gidnumber = []
@@ -22,110 +48,23 @@ module Tapjoy
22
48
  results.each { |result| gidnumber = result.gidnumber }
23
49
  return gidnumber[0]
24
50
  end
25
- end
26
51
 
27
- attr_reader :groupname, :servers, :conn
28
-
29
- # Instantiate class
30
- def initialize
31
- # This is a necessary construct, because init could be called from
32
- # places other than the commandline. As result, we want to overload
33
- # init, without *really* overloading it.
34
- if ARGV.length >= 1
35
- command = ARGV.shift
36
-
37
- case command
38
- when 'create', 'delete', 'add_user'
39
- send(command)
40
- else
41
- raise Tapjoy::LDAP::InvalidArgument
42
- end
52
+ # Create Group
53
+ def create
54
+ group = Tapjoy::LDAP::Group::Create.new
55
+ group.create
43
56
  end
44
- end
45
-
46
- # Lookup GID for the given group
47
- # @TODO: Remove this in favor of class method
48
- def lookup_id(groupname)
49
- gidnumber = []
50
57
 
51
- oc_filter = Net::LDAP::Filter.eq('objectclass', 'posixGroup')
52
- cn_filter = Net::LDAP::Filter.eq('cn', groupname)
53
- filter = Net::LDAP::Filter.join(oc_filter, cn_filter)
54
-
55
- results = Tapjoy::LDAP::client.search(['gidNumber'], filter)
56
-
57
- # Make sure we return one, and only one group
58
- if results.size < 1
59
- abort('Group not found')
60
- elsif results.size > 1
61
- abort('Multiple groups found. Please narrow your search.')
58
+ # Delete group
59
+ def delete
60
+ group = Tapjoy::LDAP::Group::Delete.new
61
+ group.delete
62
62
  end
63
63
 
64
- results.each { |result| gidnumber = result.gidnumber }
65
- return gidnumber[0]
66
- end
67
-
68
- private
69
- # Create group
70
- def create
71
- opts = Trollop::options do
72
- # Set help message
73
- banner("#{$0} group create [options]")
74
-
75
- opt :name, 'Specify group to create', :type => :string
76
- opt :type, 'Specfy if this is a user or service group', :type => :string, :default => 'user'
64
+ def add_user
65
+ group = Tapjoy::LDAP::Group::AddUser.new
66
+ group.add_user
77
67
  end
78
-
79
- Trollop::die :name, 'argument count must be one' if opts[:name].nil?
80
- Trollop::die :type, "argument must be 'user' or 'service'" unless ['user', 'service'].include?opts[:type]
81
-
82
- dn = "cn=#{ opts[:name] },ou=Group,#{ Tapjoy::LDAP::client.basedn }"
83
-
84
- ldap_attr = {
85
- :cn => opts[:name],
86
- :objectclass => ['top','posixGroup'],
87
- :gidnumber => Tapjoy::LDAP::client.get_max_id('group', opts[:type])
88
- }
89
- puts Tapjoy::LDAP::client.add(dn, ldap_attr)
90
- end
91
-
92
- # Delete group
93
- def delete
94
- opts = Trollop::options do
95
- # Set help message
96
- banner("#{$0} group delete [options]")
97
-
98
- opt(:group, 'Specify group', :type => :string, :required => true)
99
- opt(:force, 'Force delete')
100
- end
101
-
102
- dn = "cn=#{ opts[:group] },ou=Group,#{ Tapjoy::LDAP::client.basedn }"
103
- unless opts[:force]
104
- puts "Confirm that you want to delete group: #{ opts[:group] }"
105
- print '>'
106
- confirm = STDIN.gets.chomp().downcase
107
- unless confirm.eql?('y') || confirm.eql?('yes')
108
- abort("Deletion of #{ opts[:group] } aborted")
109
- end
110
- end
111
-
112
- puts Tapjoy::LDAP::client.delete(dn)
113
- end
114
-
115
- # Add user to group
116
- def add_user
117
- opts = Trollop::options do
118
- banner("#{0} group add_user [options]")
119
-
120
- opt(:group, 'Specify group', :type => :string, :required => true)
121
- opt(:username, 'Specify username', :type => :string, :required => true)
122
- end
123
-
124
- dn = "cn=#{ opts[:group] },ou=Group,#{ Tapjoy::LDAP::client.basedn }"
125
- operations = [
126
- [:add, :memberUid, opts[:username]]
127
- ]
128
- puts Tapjoy::LDAP::client.modify(dn, operations)
129
68
  end
130
69
  end
131
70
  end
@@ -3,6 +3,7 @@ require 'securerandom'
3
3
  module Tapjoy
4
4
  module LDAP
5
5
  module User
6
+ # Create LDAP user
6
7
  class Create
7
8
  def opts
8
9
  @opts ||= Trollop::options do
@@ -28,23 +29,12 @@ module Tapjoy
28
29
  def gidnumber
29
30
  @gidnumber ||= Tapjoy::LDAP::Group.lookup_id(opts[:group])
30
31
  end
31
-
32
+
32
33
  def create
33
34
  # Check for errors
34
35
  Trollop::die :user, 'argument count must be two' if opts[:user].size != 2
35
36
  Trollop::die :type, "argument must be 'user' or 'service'" unless ['user', 'service'].include?opts[:type]
36
37
 
37
-
38
- case opts[:type]
39
- when 'user'
40
- ou = 'People'
41
- when 'service'
42
- ou = Tapjoy::LDAP::client.service_ou
43
- else
44
- puts 'Unknown type'
45
- end
46
-
47
- dn = "uid=#{ username },ou=#{ou},#{ Tapjoy::LDAP::client.basedn }"
48
38
  puts Tapjoy::LDAP::client.add(dn, ldap_attr)
49
39
  end
50
40
 
@@ -77,6 +67,23 @@ module Tapjoy
77
67
  :userpassword => '{SSHA}' + create_password
78
68
  }
79
69
  end
70
+
71
+ def dn
72
+ @dn ||= "uid=#{username},ou=#{ou},#{Tapjoy::LDAP::client.basedn}"
73
+ end
74
+
75
+ def ou
76
+ @ou ||= begin
77
+ case opts[:type]
78
+ when 'user'
79
+ ou = 'People'
80
+ when 'service'
81
+ ou = Tapjoy::LDAP::client.service_ou
82
+ else
83
+ puts 'Unknown type'
84
+ end
85
+ end
86
+ end
80
87
  end
81
88
  end
82
89
  end
@@ -1,6 +1,7 @@
1
1
  module Tapjoy
2
2
  module LDAP
3
3
  module User
4
+ # Delete LDAP user
4
5
  class Delete
5
6
  def opts
6
7
  @opts ||= Trollop::options do
@@ -3,6 +3,7 @@ require_relative 'user/delete'
3
3
 
4
4
  module Tapjoy
5
5
  module LDAP
6
+ # Entry point for all user subcommands
6
7
  module User
7
8
  class << self
8
9
  SUB_COMMANDS = %w(create delete)
@@ -38,27 +39,3 @@ module Tapjoy
38
39
  end
39
40
  end
40
41
  end
41
- #
42
- # # Instantiate class
43
- # def initialize
44
- # command = ARGV.shift
45
- #
46
- # case command
47
- # when 'create', 'delete'
48
- # send(command)
49
- # else
50
- # raise Tapjoy::LDAP::InvalidArgument
51
- # end
52
- # end
53
- #
54
- # private
55
- # # Create user in LDAP
56
-
57
- #
58
- # # Delete user from LDAP
59
-
60
- # end
61
- #
62
- # end
63
- # end
64
- # end
@@ -2,7 +2,7 @@ module Tapjoy
2
2
  module LDAP
3
3
  module Version
4
4
  MAJOR = 0
5
- MINOR = 3
5
+ MINOR = 4
6
6
  PATCH = 0
7
7
  end
8
8
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ldap_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ali Tayarani
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-07 00:00:00.000000000 Z
11
+ date: 2015-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: trollop