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
1
Merge Requests
1
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-ce
Commits
eb05bdc6
Commit
eb05bdc6
authored
Aug 28, 2017
by
Nick Thomas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move the key restriction validation to its own class
parent
b84ca08e
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
33 additions
and
10 deletions
+33
-10
app/models/application_setting.rb
app/models/application_setting.rb
+2
-8
app/validators/key_restriction_validator.rb
app/validators/key_restriction_validator.rb
+29
-0
lib/api/settings.rb
lib/api/settings.rb
+1
-1
spec/models/application_setting_spec.rb
spec/models/application_setting_spec.rb
+1
-1
No files found.
app/models/application_setting.rb
View file @
eb05bdc6
...
...
@@ -15,13 +15,9 @@ class ApplicationSetting < ActiveRecord::Base
# Setting a key restriction to `-1` means that all keys of this type are
# forbidden.
FORBIDDEN_KEY_VALUE
=
-
1
FORBIDDEN_KEY_VALUE
=
KeyRestrictionValidator
::
FORBIDDEN
SUPPORTED_KEY_TYPES
=
%i[rsa dsa ecdsa ed25519]
.
freeze
def
self
.
supported_key_restrictions
(
type
)
[
0
,
*
Gitlab
::
SSHPublicKey
.
supported_sizes
(
type
),
FORBIDDEN_KEY_VALUE
]
end
serialize
:restricted_visibility_levels
# rubocop:disable Cop/ActiveRecordSerialize
serialize
:import_sources
# rubocop:disable Cop/ActiveRecordSerialize
serialize
:disabled_oauth_sign_in_sources
,
Array
# rubocop:disable Cop/ActiveRecordSerialize
...
...
@@ -156,9 +152,7 @@ class ApplicationSetting < ActiveRecord::Base
numericality:
{
greater_than_or_equal_to:
0
}
SUPPORTED_KEY_TYPES
.
each
do
|
type
|
validates
:"
#{
type
}
_key_restriction"
,
presence:
true
,
inclusion:
{
in:
ApplicationSetting
.
supported_key_restrictions
(
type
)
}
validates
:"
#{
type
}
_key_restriction"
,
presence:
true
,
key_restriction:
{
type:
type
}
end
validates_each
:restricted_visibility_levels
do
|
record
,
attr
,
value
|
...
...
app/validators/key_restriction_validator.rb
0 → 100644
View file @
eb05bdc6
class
KeyRestrictionValidator
<
ActiveModel
::
EachValidator
FORBIDDEN
=
-
1
def
self
.
supported_sizes
(
type
)
Gitlab
::
SSHPublicKey
.
supported_sizes
(
type
)
end
def
self
.
supported_key_restrictions
(
type
)
[
0
,
*
supported_sizes
(
type
),
FORBIDDEN
]
end
def
validate_each
(
record
,
attribute
,
value
)
unless
valid_restriction?
(
value
)
record
.
errors
.
add
(
attribute
,
"must be forbidden, allowed, or one of these sizes:
#{
supported_sizes_message
}
"
)
end
end
private
def
supported_sizes_message
sizes
=
self
.
class
.
supported_sizes
(
options
[
:type
])
sizes
.
to_sentence
(
last_word_connector:
', or '
,
two_words_connector:
' or '
)
end
def
valid_restriction?
(
value
)
choices
=
self
.
class
.
supported_key_restrictions
(
options
[
:type
])
choices
.
include?
(
value
)
end
end
lib/api/settings.rb
View file @
eb05bdc6
...
...
@@ -125,7 +125,7 @@ module API
ApplicationSetting
::
SUPPORTED_KEY_TYPES
.
each
do
|
type
|
optional
:"
#{
type
}
_key_restriction"
,
type:
Integer
,
values:
ApplicationSetting
.
supported_key_restrictions
(
type
),
values:
KeyRestrictionValidator
.
supported_key_restrictions
(
type
),
desc:
"Restrictions on the complexity of uploaded
#{
type
.
upcase
}
keys. A value of
#{
ApplicationSetting
::
FORBIDDEN_KEY_VALUE
}
disables all
#{
type
.
upcase
}
keys."
end
...
...
spec/models/application_setting_spec.rb
View file @
eb05bdc6
...
...
@@ -85,7 +85,7 @@ describe ApplicationSetting do
let
(
:field
)
{
:"
#{
type
}
_key_restriction"
}
it
{
is_expected
.
to
validate_presence_of
(
field
)
}
it
{
is_expected
.
to
allow_value
(
*
described_class
.
supported_key_restrictions
(
type
)).
for
(
field
)
}
it
{
is_expected
.
to
allow_value
(
*
KeyRestrictionValidator
.
supported_key_restrictions
(
type
)).
for
(
field
)
}
it
{
is_expected
.
not_to
allow_value
(
128
).
for
(
field
)
}
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