Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
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
Léo-Paul Géneau
gitlab-ce
Commits
fbf1fd1a
Commit
fbf1fd1a
authored
Feb 22, 2017
by
Alexis Reigel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add gpg key model
parent
28bb5e3d
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
100 additions
and
0 deletions
+100
-0
app/models/gpg_key.rb
app/models/gpg_key.rb
+35
-0
db/migrate/20170222111732_create_gpg_keys.rb
db/migrate/20170222111732_create_gpg_keys.rb
+13
-0
db/schema.rb
db/schema.rb
+11
-0
spec/models/gpg_key_spec.rb
spec/models/gpg_key_spec.rb
+41
-0
No files found.
app/models/gpg_key.rb
0 → 100644
View file @
fbf1fd1a
class
GpgKey
<
ActiveRecord
::
Base
KEY_PREFIX
=
'-----BEGIN PGP PUBLIC KEY BLOCK-----'
.
freeze
belongs_to
:user
validates
:fingerprint
,
presence:
true
,
uniqueness:
true
validates
:key
,
presence:
true
,
uniqueness:
true
,
format:
{
with:
/\A
#{
KEY_PREFIX
}
((?!
#{
KEY_PREFIX
}
).)+\Z/m
}
before_validation
:extract_fingerprint
def
key
=
(
value
)
value
.
strip!
unless
value
.
blank?
write_attribute
(
:key
,
value
)
end
private
def
extract_fingerprint
import
=
GPGME
::
Key
.
import
(
key
)
return
if
import
.
considered
==
0
# we can assume that the result only contains one item as the validation
# only allows one key
self
.
fingerprint
=
import
.
imports
.
first
.
fingerprint
end
end
db/migrate/20170222111732_create_gpg_keys.rb
0 → 100644
View file @
fbf1fd1a
class
CreateGpgKeys
<
ActiveRecord
::
Migration
DOWNTIME
=
false
def
change
create_table
:gpg_keys
do
|
t
|
t
.
string
:fingerprint
t
.
text
:key
t
.
references
:user
,
index:
true
,
foreign_key:
true
t
.
timestamps_with_timezone
null:
false
end
end
end
db/schema.rb
View file @
fbf1fd1a
...
...
@@ -540,6 +540,16 @@ ActiveRecord::Schema.define(version: 20170725145659) do
add_index
"forked_project_links"
,
[
"forked_to_project_id"
],
name:
"index_forked_project_links_on_forked_to_project_id"
,
unique:
true
,
using: :btree
create_table
"gpg_keys"
,
force: :cascade
do
|
t
|
t
.
string
"fingerprint"
t
.
text
"key"
t
.
integer
"user_id"
t
.
datetime
"created_at"
,
null:
false
t
.
datetime
"updated_at"
,
null:
false
end
add_index
"gpg_keys"
,
[
"user_id"
],
name:
"index_gpg_keys_on_user_id"
,
using: :btree
create_table
"identities"
,
force: :cascade
do
|
t
|
t
.
string
"extern_uid"
t
.
string
"provider"
...
...
@@ -1602,6 +1612,7 @@ ActiveRecord::Schema.define(version: 20170725145659) do
add_foreign_key
"environments"
,
"projects"
,
name:
"fk_d1c8c1da6a"
,
on_delete: :cascade
add_foreign_key
"events"
,
"projects"
,
name:
"fk_0434b48643"
,
on_delete: :cascade
add_foreign_key
"forked_project_links"
,
"projects"
,
column:
"forked_to_project_id"
,
name:
"fk_434510edb0"
,
on_delete: :cascade
add_foreign_key
"gpg_keys"
,
"users"
add_foreign_key
"issue_assignees"
,
"issues"
,
name:
"fk_b7d881734a"
,
on_delete: :cascade
add_foreign_key
"issue_assignees"
,
"users"
,
name:
"fk_5e0c8d9154"
,
on_delete: :cascade
add_foreign_key
"issue_metrics"
,
"issues"
,
on_delete: :cascade
...
...
spec/models/gpg_key_spec.rb
0 → 100644
View file @
fbf1fd1a
require
'rails_helper'
describe
GpgKey
do
describe
"associations"
do
it
{
is_expected
.
to
belong_to
(
:user
)
}
end
describe
"validation"
do
it
{
is_expected
.
to
validate_presence_of
(
:fingerprint
)
}
it
{
is_expected
.
to
validate_presence_of
(
:key
)
}
it
{
is_expected
.
to
validate_uniqueness_of
(
:key
)
}
it
{
is_expected
.
to
allow_value
(
"-----BEGIN PGP PUBLIC KEY BLOCK-----
\n
key"
).
for
(
:key
)
}
it
{
is_expected
.
not_to
allow_value
(
"-----BEGIN PGP PUBLIC KEY BLOCK-----
\n
key
\n
-----BEGIN PGP PUBLIC KEY BLOCK-----"
).
for
(
:key
)
}
it
{
is_expected
.
not_to
allow_value
(
'BEGIN PGP'
).
for
(
:key
)
}
end
context
'callbacks'
do
describe
'extract_fingerprint'
do
it
'extracts the fingerprint from the gpg key'
,
:gpg
do
gpg_key
=
described_class
.
new
(
key:
GpgHelpers
.
public_key
)
gpg_key
.
valid?
expect
(
gpg_key
.
fingerprint
).
to
eq
'4F4840A503964251CF7D7F5DC728AF10972E97C0'
end
end
end
describe
'#key='
do
it
'strips white spaces'
do
key
=
<<~
KEY
.
strip
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1
mQENBFMOSOgBCADFCYxmnXFbrDhfvlf03Q/bQuT+nZu46BFGbo7XkUjDowFXJQhP
-----END PGP PUBLIC KEY BLOCK-----
KEY
expect
(
described_class
.
new
(
key:
"
#{
key
}
"
).
key
).
to
eq
(
key
)
end
end
end
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