Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
keystore
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
id
keystore
Commits
ec1bcfe0
Commit
ec1bcfe0
authored
7 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Do not return an error when a user has no encryption keys on Open
And make HTTP result codes slightly more meaningful.
parent
3dff474e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+5
-2
5 additions, 2 deletions
README.md
server/keystore.go
+14
-8
14 additions, 8 deletions
server/keystore.go
server/server.go
+10
-2
10 additions, 2 deletions
server/server.go
with
29 additions
and
12 deletions
README.md
+
5
−
2
View file @
ec1bcfe0
...
...
@@ -27,14 +27,17 @@ similarly JSON-encoded.
Retrieve the encrypted key for a user, decrypt it with the provided
password, and store it in memory.
OpenRequest is an object with the
following attributes:
OpenRequest is an object with the following attributes:
*
`username`
*
`password`
to decrypt the user's key with
*
`ttl`
(seconds) time after which the credentials are automatically
forgotten
If the user has no encrypted keys in the database, the request will
still return successfully: no action will be performed, and no errors
will be returned.
`/api/get`
(
*GetRequest*
) ->
*GetResponse*
Retrieve the key for a user. GetRequest must contain the following
...
...
This diff is collapsed.
Click to expand it.
server/keystore.go
+
14
−
8
View file @
ec1bcfe0
...
...
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"io/ioutil"
"log"
"strings"
"sync"
"time"
...
...
@@ -15,9 +16,10 @@ import (
)
var
(
ErrNoKeys
=
errors
.
New
(
"no keys available"
)
ErrBadUser
=
errors
.
New
(
"username does not match authentication token"
)
ErrInvalidTTL
=
errors
.
New
(
"invalid ttl"
)
errNoKeys
=
errors
.
New
(
"no keys available"
)
errBadUser
=
errors
.
New
(
"username does not match authentication token"
)
errUnauthorized
=
errors
.
New
(
"unauthorized"
)
errInvalidTTL
=
errors
.
New
(
"invalid ttl"
)
)
// Database represents the interface to the underlying backend for
...
...
@@ -130,7 +132,7 @@ func (s *KeyStore) expire() {
// A Context is needed because this method might issue an RPC.
func
(
s
*
KeyStore
)
Open
(
ctx
context
.
Context
,
username
,
password
string
,
ttlSeconds
int
)
error
{
if
ttlSeconds
==
0
{
return
E
rrInvalidTTL
return
e
rrInvalidTTL
}
encKeys
,
err
:=
s
.
db
.
GetPrivateKeys
(
ctx
,
username
)
...
...
@@ -138,7 +140,8 @@ func (s *KeyStore) Open(ctx context.Context, username, password string, ttlSecon
return
err
}
if
len
(
encKeys
)
==
0
{
return
ErrNoKeys
// No keys found. Not an error.
return
nil
}
// Naive and inefficient way of decrypting multiple keys: it
...
...
@@ -163,17 +166,20 @@ func (s *KeyStore) Get(username, ssoTicket string) ([]byte, error) {
// Validate the SSO ticket.
tkt
,
err
:=
s
.
validator
.
Validate
(
ssoTicket
,
""
,
s
.
service
,
nil
)
if
err
!=
nil
{
return
nil
,
err
// Log authentication failures for debugging purposes.
log
.
Printf
(
"Validate(%s) error: %v"
,
username
,
err
)
return
nil
,
errUnauthorized
}
if
tkt
.
User
!=
username
{
return
nil
,
ErrBadUser
log
.
Printf
(
"Validate(%s) user mismatch: sso=%s"
,
username
,
tkt
.
User
)
return
nil
,
errBadUser
}
s
.
mx
.
Lock
()
defer
s
.
mx
.
Unlock
()
u
,
ok
:=
s
.
userKeys
[
username
]
if
!
ok
{
return
nil
,
E
rrNoKeys
return
nil
,
e
rrNoKeys
}
return
u
.
pkey
,
nil
}
...
...
This diff is collapsed.
Click to expand it.
server/server.go
+
10
−
2
View file @
ec1bcfe0
...
...
@@ -24,7 +24,7 @@ func (s *keyStoreServer) handleOpen(w http.ResponseWriter, r *http.Request) {
err
:=
s
.
KeyStore
.
Open
(
r
.
Context
(),
req
.
Username
,
req
.
Password
,
req
.
TTL
)
if
err
!=
nil
{
log
.
Printf
(
"Open(%s) error: %v"
,
req
.
Username
,
err
)
http
.
Error
(
w
,
err
.
Error
(),
http
.
Status
Unauthorized
)
http
.
Error
(
w
,
err
.
Error
(),
http
.
Status
InternalServerError
)
return
}
...
...
@@ -40,7 +40,15 @@ func (s *keyStoreServer) handleGet(w http.ResponseWriter, r *http.Request) {
key
,
err
:=
s
.
KeyStore
.
Get
(
req
.
Username
,
req
.
SSOTicket
)
if
err
!=
nil
{
log
.
Printf
(
"Get(%s) error: %v"
,
req
.
Username
,
err
)
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusUnauthorized
)
// Return an appropriate error code.
switch
err
{
case
errUnauthorized
,
errBadUser
:
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusUnauthorized
)
case
errNoKeys
:
http
.
NotFound
(
w
,
r
)
default
:
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
}
return
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment