Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-shell
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-shell
Commits
367a672a
Commit
367a672a
authored
Mar 14, 2014
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'batch_key_import' into 'master'
Batch Key Import
parents
9e076ab2
3a48eae7
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
69 additions
and
2 deletions
+69
-2
CHANGELOG
CHANGELOG
+1
-0
bin/gitlab-keys
bin/gitlab-keys
+3
-1
lib/gitlab_keys.rb
lib/gitlab_keys.rb
+23
-1
spec/gitlab_keys_spec.rb
spec/gitlab_keys_spec.rb
+42
-0
No files found.
CHANGELOG
View file @
367a672a
v1.8.4
- Dont do import if repository exists
- Add `gitlab-keys batch-add-keys` subcommand for authorized_keys rebuilds
v1.8.3
- Add timeout option for repository import
...
...
bin/gitlab-keys
View file @
367a672a
...
...
@@ -8,9 +8,11 @@ require_relative '../lib/gitlab_init'
# Ex.
# /bin/gitlab-keys add-key key-782 "ssh-rsa AAAAx321..."
#
# printf "key-782\tssh-rsa AAAAx321...\n" | /bin/gitlab-keys batch-add-keys
#
# /bin/gitlab-keys rm-key key-23 "ssh-rsa AAAAx321..."
#
# /bin/gitlab-keys clear
"
# /bin/gitlab-keys clear
#
require
File
.
join
(
ROOT_PATH
,
'lib'
,
'gitlab_keys'
)
...
...
lib/gitlab_keys.rb
View file @
367a672a
...
...
@@ -16,6 +16,7 @@ class GitlabKeys
def
exec
case
@command
when
'add-key'
;
add_key
when
'batch-add-keys'
;
batch_add_keys
when
'rm-key'
;
rm_key
when
'clear'
;
clear
else
...
...
@@ -29,11 +30,32 @@ class GitlabKeys
def
add_key
$logger
.
info
"Adding key
#{
@key_id
}
=>
#{
@key
.
inspect
}
"
auth_line
=
"command=
\"
#{
ROOT_PATH
}
/bin/gitlab-shell
#{
@key_id
}
\"
,no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty
#{
@key
}
"
auth_line
=
key_line
(
@key_id
,
@key
)
open
(
auth_file
,
'a'
)
{
|
file
|
file
.
puts
(
auth_line
)
}
true
end
def
batch_add_keys
open
(
auth_file
,
'a'
)
do
|
file
|
stdin
.
each_line
do
|
input
|
tokens
=
input
.
strip
.
split
(
"
\t
"
)
abort
(
"
#{
$0
}
: invalid input
#{
input
.
inspect
}
"
)
unless
tokens
.
count
==
2
key_id
,
public_key
=
tokens
$logger
.
info
"Adding key
#{
key_id
}
=>
#{
public_key
.
inspect
}
"
file
.
puts
(
key_line
(
key_id
,
public_key
))
end
end
true
end
def
stdin
$stdin
end
def
key_line
(
key_id
,
public_key
)
auth_line
=
"command=
\"
#{
ROOT_PATH
}
/bin/gitlab-shell
#{
key_id
}
\"
,no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty
#{
public_key
}
"
end
def
rm_key
$logger
.
info
"Removing key
#{
@key_id
}
"
Tempfile
.
open
(
'authorized_keys'
)
do
|
temp
|
...
...
spec/gitlab_keys_spec.rb
View file @
367a672a
require_relative
'spec_helper'
require_relative
'../lib/gitlab_keys'
require
'stringio'
describe
GitlabKeys
do
before
do
...
...
@@ -39,6 +40,47 @@ describe GitlabKeys do
end
end
describe
:batch_add_keys
do
let
(
:gitlab_keys
)
{
build_gitlab_keys
(
'batch-add-keys'
)
}
let
(
:fake_stdin
)
{
StringIO
.
new
(
"key-12
\t
ssh-dsa ASDFASGADG
\n
key-123
\t
ssh-rsa GFDGDFSGSDFG
\n
"
,
'r'
)
}
before
do
create_authorized_keys_fixture
gitlab_keys
.
stub
(
stdin:
fake_stdin
)
end
it
"adds lines at the end of the file"
do
gitlab_keys
.
send
:batch_add_keys
auth_line1
=
"command=
\"
#{
ROOT_PATH
}
/bin/gitlab-shell key-12
\"
,no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-dsa ASDFASGADG"
auth_line2
=
"command=
\"
#{
ROOT_PATH
}
/bin/gitlab-shell key-123
\"
,no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa GFDGDFSGSDFG"
File
.
read
(
tmp_authorized_keys_path
).
should
==
"existing content
\n
#{
auth_line1
}
\n
#{
auth_line2
}
\n
"
end
context
"with invalid input"
do
let
(
:fake_stdin
)
{
StringIO
.
new
(
"key-12
\t
ssh-dsa ASDFASGADG
\n
key-123
\t
ssh-rsa GFDGDFSGSDFG
\n
foo
\t
bar
\t
baz
\n
"
,
'r'
)
}
it
"aborts"
do
gitlab_keys
.
should_receive
(
:abort
)
gitlab_keys
.
send
:batch_add_keys
end
end
context
"without file writing"
do
before
do
gitlab_keys
.
should_receive
(
:open
).
and_yield
(
mock
(
:file
,
puts:
nil
))
end
it
"should log an add-key event"
do
$logger
.
should_receive
(
:info
).
with
(
'Adding key key-12 => "ssh-dsa ASDFASGADG"'
)
$logger
.
should_receive
(
:info
).
with
(
'Adding key key-123 => "ssh-rsa GFDGDFSGSDFG"'
)
gitlab_keys
.
send
:batch_add_keys
end
it
"should return true"
do
gitlab_keys
.
send
(
:batch_add_keys
).
should
be_true
end
end
end
describe
:rm_key
do
let
(
:gitlab_keys
)
{
build_gitlab_keys
(
'rm-key'
,
'key-741'
,
'ssh-rsa AAAAB3NzaDAxx2E'
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment