-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c078fe
commit 38b598c
Showing
27 changed files
with
1,380 additions
and
10 deletions.
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
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,189 @@ | ||
package handler | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/productiveops/dokemon/pkg/server/model" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func (h *Handler) CreateCredential(c echo.Context) error { | ||
m := model.Credential{} | ||
r := &credentialCreateRequest{} | ||
if err := r.bind(c, &m); err != nil { | ||
return unprocessableEntity(c, err) | ||
} | ||
|
||
isUnique, err := h.credentialStore.IsUniqueName(r.Name) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if !isUnique { | ||
return unprocessableEntity(c, duplicateNameError()) | ||
} | ||
|
||
if err := h.credentialStore.Create(&m); err != nil { | ||
panic(err) | ||
} | ||
|
||
return created(c, m.Id) | ||
} | ||
|
||
func (h *Handler) UpdateCredentialDetails(c echo.Context) error { | ||
id, err := strconv.Atoi(c.Param("id")) | ||
if err != nil { | ||
return unprocessableEntity(c, routeIntExpectedError("id")) | ||
} | ||
|
||
m, err := h.credentialStore.GetById(uint(id)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if m == nil { | ||
return resourceNotFound(c, "Credential") | ||
} | ||
|
||
r := &credentialUpdateDetailsRequest{Id: uint(id)} | ||
if err := r.bind(c, m); err != nil { | ||
return unprocessableEntity(c, err) | ||
} | ||
|
||
isUnique, err := h.credentialStore.IsUniqueNameExcludeItself(r.Name, r.Id) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if !isUnique { | ||
return unprocessableEntity(c, duplicateNameError()) | ||
} | ||
|
||
if err := h.credentialStore.Update(m); err != nil { | ||
panic(err) | ||
} | ||
|
||
return noContent(c) | ||
} | ||
|
||
func (h *Handler) UpdateCredentialSecret(c echo.Context) error { | ||
id, err := strconv.Atoi(c.Param("id")) | ||
if err != nil { | ||
return unprocessableEntity(c, routeIntExpectedError("id")) | ||
} | ||
|
||
m, err := h.credentialStore.GetById(uint(id)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if m == nil { | ||
return resourceNotFound(c, "Credential") | ||
} | ||
|
||
r := &credentialUpdateSecretRequest{Id: uint(id)} | ||
if err := r.bind(c, m); err != nil { | ||
return unprocessableEntity(c, err) | ||
} | ||
|
||
if err := h.credentialStore.Update(m); err != nil { | ||
panic(err) | ||
} | ||
|
||
return noContent(c) | ||
} | ||
|
||
func (h *Handler) DeleteCredentialById(c echo.Context) error { | ||
id, err := strconv.Atoi(c.Param("id")) | ||
if err != nil { | ||
return unprocessableEntity(c, routeIntExpectedError("id")) | ||
} | ||
|
||
exists, err := h.credentialStore.Exists(uint(id)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if !exists { | ||
return resourceNotFound(c, "Credential") | ||
} | ||
|
||
if err := h.credentialStore.DeleteById(uint(id)); err != nil { | ||
panic(err) | ||
} | ||
|
||
return noContent(c) | ||
} | ||
|
||
func (h *Handler) GetCredentialList(c echo.Context) error { | ||
p, err := strconv.Atoi(c.QueryParam("p")) | ||
if err != nil { | ||
return unprocessableEntity(c, routeIntExpectedError("p")) | ||
} | ||
|
||
if p < 1 { | ||
return unprocessableEntity(c, queryGte1ExpectedError("p")) | ||
} | ||
|
||
s, err := strconv.Atoi(c.QueryParam("s")) | ||
if err != nil { | ||
return unprocessableEntity(c, routeIntExpectedError("s")) | ||
} | ||
|
||
if s < 1 { | ||
return unprocessableEntity(c, queryGte1ExpectedError("s")) | ||
} | ||
|
||
rows, totalRows, err := h.credentialStore.GetList(uint(p), uint(s)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return ok(c, newPageResponse[credentialHead](newCredentialHeadList(rows), uint(p), uint(s), uint(totalRows))) | ||
} | ||
|
||
func (h *Handler) GetCredentialById(c echo.Context) error { | ||
id, err := strconv.Atoi(c.Param("id")) | ||
if err != nil { | ||
return unprocessableEntity(c, routeIntExpectedError("id")) | ||
} | ||
|
||
m, err := h.credentialStore.GetById(uint(id)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if m == nil { | ||
return resourceNotFound(c, "Credential") | ||
} | ||
|
||
return ok(c, newCredentialResponse(m)) | ||
} | ||
|
||
func (h *Handler) IsUniqueCredentialName(c echo.Context) error { | ||
value := c.QueryParam("value") | ||
|
||
unique, err := h.credentialStore.IsUniqueName(value) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return ok(c, newUniqueResponse(unique)) | ||
} | ||
|
||
func (h *Handler) IsUniqueCredentialNameExcludeItself(c echo.Context) error { | ||
id, err := strconv.Atoi(c.Param("id")) | ||
if err != nil { | ||
return unprocessableEntity(c, routeIntExpectedError("id")) | ||
} | ||
|
||
value := c.QueryParam("value") | ||
|
||
unique, err := h.credentialStore.IsUniqueNameExcludeItself(value, uint(id)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return ok(c, newUniqueResponse(unique)) | ||
} |
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,82 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"strings" | ||
|
||
"github.com/google/go-github/v57/github" | ||
"github.com/labstack/echo/v4" | ||
"github.com/productiveops/dokemon/pkg/crypto/ske" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type gitHubUrlParts struct { | ||
Owner string | ||
Repo string | ||
Ref string | ||
Path string | ||
} | ||
|
||
func getGitHubUrlParts(url string) (*gitHubUrlParts, error) { | ||
ret := &gitHubUrlParts{} | ||
|
||
if !strings.HasPrefix(url, "https://github.com") { | ||
return nil, errors.New("URL should begin with https://github.com") | ||
} | ||
|
||
parts := strings.Split(url[19:], "/") | ||
if len(parts) < 5 { | ||
return nil, errors.New("URL should be of format: https://github.com/OWNER/REPO/blob/REF/path/to/filename.extension") | ||
} | ||
|
||
ret.Owner = parts[0] | ||
ret.Repo = parts[1] | ||
ret.Ref = parts[3] | ||
ret.Path = strings.Join(parts[4:], "/") | ||
|
||
return ret, nil | ||
} | ||
|
||
func (h *Handler) RetrieveGitHubFileContent(c echo.Context) error { | ||
r := &gitHubfileContentRetrieveRequest{} | ||
if err := r.bind(c); err != nil { | ||
return unprocessableEntity(c, err) | ||
} | ||
|
||
client := github.NewClient(nil) | ||
if r.CredentialId != nil { | ||
credential, err := h.credentialStore.GetById(*r.CredentialId) | ||
if err != nil { | ||
return unprocessableEntity(c, errors.New("Credentials not found")) | ||
} | ||
|
||
decryptedSecret, err := ske.Decrypt(credential.Secret) | ||
if err != nil { | ||
panic(err) | ||
} | ||
client = github.NewClient(nil).WithAuthToken(decryptedSecret) | ||
} | ||
|
||
p, err := getGitHubUrlParts(r.Url) | ||
if err != nil { | ||
return unprocessableEntity(c, err) | ||
} | ||
|
||
content, _, _, err := client.Repositories.GetContents(context.Background(), p.Owner, p.Repo, p.Path, &github.RepositoryContentGetOptions{Ref: p.Ref}) | ||
if err != nil { | ||
if r.CredentialId != nil { | ||
log.Error().Err(err).Str("url", r.Url).Uint("credentialId", *r.CredentialId).Msg("Error while retriveing file content from GitHub") | ||
} else { | ||
log.Error().Err(err).Str("url", r.Url).Msg("Error while retriveing file content from GitHub") | ||
} | ||
return unprocessableEntity(c, errors.New("Error while retrieving file content from provided GitHub URL")) | ||
} | ||
|
||
text, err := content.GetContent() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return ok(c, newGitHubfileContentResponse(&text)) | ||
} |
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
Oops, something went wrong.