-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Persistent tokens and hook deletion #22
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d47222a
[run] Use 'date +%s' for temporary test dir suffix
achilleas-k b52d782
Store tokens to disk and use for cloning
achilleas-k 4665f31
Move token saving and loading to separate file
achilleas-k d60cf3f
[user] Retrieve existing token if available
achilleas-k b4fcf52
[token] Function stubs for session/repo->token mapping
achilleas-k d0006f0
Link session ID to token and read it when needed
achilleas-k 1c40a4b
Rename function: validateHookSecret -> checkHookSecret
achilleas-k 680e0b0
Link repopath to token and read it when needed
achilleas-k d1ac53c
[token] Implement token linking and loading stubs
achilleas-k eabed3b
Base32 encode session ID and repopath for token store
achilleas-k d4698be
Store the token file
achilleas-k c0b76cb
Use SymLinks instead of hard Links for token by id or repo
achilleas-k 11e4546
Support hook deletion and hook state enum
achilleas-k cd78d13
[travis] Add go 1.11 to build matrix
achilleas-k 2b274a0
[web] Log all redirects to login
achilleas-k 2b0c48e
Rename loadUserToken to getTokenByUsername
achilleas-k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ sudo: required | |
matrix: | ||
include: | ||
- go: "1.10.x" | ||
- go: "1.11.x" | ||
- go: tip | ||
allow_failures: | ||
- go: tip | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package web | ||
|
||
import ( | ||
"encoding/base32" | ||
"encoding/gob" | ||
"os" | ||
"path/filepath" | ||
|
||
gweb "github.com/G-Node/gin-cli/web" | ||
"github.com/G-Node/gin-valid/config" | ||
"github.com/G-Node/gin-valid/log" | ||
) | ||
|
||
// saveToken writes a token to disk using the username as filename. | ||
// The location is defined by config.Dir.Tokens. | ||
func saveToken(ut gweb.UserToken) error { | ||
cfg := config.Read() | ||
filename := filepath.Join(cfg.Dir.Tokens, ut.Username) | ||
tokenfile, err := os.Create(filename) | ||
defer tokenfile.Close() | ||
if err != nil { | ||
return err | ||
} | ||
encoder := gob.NewEncoder(tokenfile) | ||
return encoder.Encode(ut) | ||
} | ||
|
||
// getTokenByUsername reads a token from disk using the username as filename. | ||
// The location is defined by config.Dir.Tokens. | ||
func getTokenByUsername(username string) (gweb.UserToken, error) { | ||
cfg := config.Read() | ||
filename := filepath.Join(cfg.Dir.Tokens, username) | ||
return loadToken(filename) | ||
} | ||
|
||
// loadToken loads a token from the provided path | ||
func loadToken(path string) (gweb.UserToken, error) { | ||
ut := gweb.UserToken{} | ||
tokenfile, err := os.Open(path) | ||
if err != nil { | ||
log.Write("[Error] Failed to load token from %s", path) | ||
return ut, err | ||
} | ||
defer tokenfile.Close() | ||
|
||
decoder := gob.NewDecoder(tokenfile) | ||
err = decoder.Decode(&ut) | ||
return ut, err | ||
} | ||
|
||
// linkToSession links a sessionID to a user's token. | ||
func linkToSession(username string, sessionid string) error { | ||
cfg := config.Read() | ||
tokendir := cfg.Dir.Tokens | ||
utfile := filepath.Join(tokendir, username) | ||
sidfile := filepath.Join(tokendir, "by-sessionid", b32(sessionid)) | ||
return os.Symlink(utfile, sidfile) | ||
} | ||
|
||
// getTokenBySession loads a user's access token using the session ID found in | ||
// the user's cookie store. | ||
func getTokenBySession(sessionid string) (gweb.UserToken, error) { | ||
cfg := config.Read() | ||
tokendir := cfg.Dir.Tokens | ||
filename := filepath.Join(tokendir, "by-sessionid", b32(sessionid)) | ||
return loadToken(filename) | ||
} | ||
|
||
// linkToRepo links a repository name to a user's token. | ||
// This token will be used for cloning a repository to run a validator when a | ||
// web hook is triggered. | ||
func linkToRepo(username string, repopath string) error { | ||
cfg := config.Read() | ||
tokendir := cfg.Dir.Tokens | ||
utfile := filepath.Join(tokendir, username) | ||
sidfile := filepath.Join(tokendir, "by-repo", b32(repopath)) | ||
return os.Symlink(utfile, sidfile) | ||
} | ||
|
||
// getTokenByRepo loads a user's access token using a repository path. | ||
func getTokenByRepo(repopath string) (gweb.UserToken, error) { | ||
cfg := config.Read() | ||
tokendir := cfg.Dir.Tokens | ||
filename := filepath.Join(tokendir, "by-repo", b32(repopath)) | ||
return loadToken(filename) | ||
} | ||
|
||
// rmTokenRepoLink deletes a repository -> token link, removing our ability to | ||
// clone the repository. | ||
func rmTokenRepoLink(repopath string) error { | ||
cfg := config.Read() | ||
tokendir := cfg.Dir.Tokens | ||
filename := filepath.Join(tokendir, "by-repo", b32(repopath)) | ||
return os.Remove(filename) | ||
} | ||
|
||
// b32 encodes a string to base 32. Use this to make strings such as IDs or | ||
// repopaths filename friendly. | ||
func b32(s string) string { | ||
return base32.StdEncoding.EncodeToString([]byte(s)) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are tokens and session symlinks ever cleaned up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope. Opened issue #23.