-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #323 from jmpsec/cli-login
Login to `osctrl-api` using `osctrl-cli`
- Loading branch information
Showing
14 changed files
with
313 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,9 @@ notes.txt | |
# Tests | ||
coverage.out | ||
|
||
# Configuration | ||
osctrl-api.json | ||
|
||
# Go Workspace | ||
go.work | ||
go.work.sum |
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 main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/jmpsec/osctrl/settings" | ||
"github.com/jmpsec/osctrl/types" | ||
"github.com/jmpsec/osctrl/users" | ||
"github.com/jmpsec/osctrl/utils" | ||
) | ||
|
||
const ( | ||
metricAPILoginReq = "login-req" | ||
metricAPILoginErr = "login-err" | ||
metricAPILoginOK = "login-ok" | ||
) | ||
|
||
// POST Handler for API login request | ||
func apiLoginHandler(w http.ResponseWriter, r *http.Request) { | ||
incMetric(metricAPILoginReq) | ||
utils.DebugHTTPDump(r, settingsmgr.DebugHTTP(settings.ServiceAPI), false) | ||
vars := mux.Vars(r) | ||
// Extract environment | ||
envVar, ok := vars["env"] | ||
if !ok { | ||
apiErrorResponse(w, "error with environment", http.StatusInternalServerError, nil) | ||
incMetric(metricAPILoginErr) | ||
return | ||
} | ||
// Get environment | ||
env, err := envs.Get(envVar) | ||
if err != nil { | ||
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil) | ||
incMetric(metricAPILoginErr) | ||
return | ||
} | ||
var l types.ApiLoginRequest | ||
// Parse request JSON body | ||
if err := json.NewDecoder(r.Body).Decode(&l); err != nil { | ||
apiErrorResponse(w, "error parsing POST body", http.StatusInternalServerError, err) | ||
incMetric(metricAPILoginErr) | ||
return | ||
} | ||
// Check credentials | ||
access, user := apiUsers.CheckLoginCredentials(l.Username, l.Password) | ||
if !access { | ||
apiErrorResponse(w, "invalid credentials", http.StatusForbidden, err) | ||
incMetric(metricAPILoginErr) | ||
return | ||
} | ||
// Check if user has access to this environment | ||
if !apiUsers.CheckPermissions(l.Username, users.AdminLevel, env.UUID) { | ||
apiErrorResponse(w, "no access", http.StatusForbidden, fmt.Errorf("attempt to use API by user %s", l.Username)) | ||
incMetric(metricAPILoginErr) | ||
return | ||
} | ||
// Do we have a token already? | ||
if user.APIToken == "" { | ||
token, exp, err := apiUsers.CreateToken(l.Username) | ||
if err != nil { | ||
apiErrorResponse(w, "error creating token", http.StatusInternalServerError, err) | ||
incMetric(metricAPILoginErr) | ||
return | ||
} | ||
if err = apiUsers.UpdateToken(l.Username, token, exp); err != nil { | ||
apiErrorResponse(w, "error updating token", http.StatusInternalServerError, err) | ||
incMetric(metricAPILoginErr) | ||
return | ||
} | ||
user.APIToken = token | ||
} | ||
// Serialize and serve JSON | ||
if settingsmgr.DebugService(settings.ServiceAPI) { | ||
log.Printf("DebugService: Returning token for %s", user.Username) | ||
} | ||
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, types.ApiLoginResponse{Token: user.APIToken}) | ||
incMetric(metricAPILoginOK) | ||
} |
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,32 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/jmpsec/osctrl/types" | ||
) | ||
|
||
// PostLogin to login into API to retrieve a token | ||
func (api *OsctrlAPI) PostLogin(env, username, password string) (types.ApiLoginResponse, error) { | ||
var res types.ApiLoginResponse | ||
l := types.ApiLoginRequest{ | ||
Username: username, | ||
Password: password, | ||
} | ||
jsonMessage, err := json.Marshal(l) | ||
if err != nil { | ||
return res, fmt.Errorf("error marshaling data %s", err) | ||
} | ||
jsonParam := strings.NewReader(string(jsonMessage)) | ||
reqURL := fmt.Sprintf("%s%s%s/%s", api.Configuration.URL, APIPath, APILogin, env) | ||
rawRes, err := api.PostGeneric(reqURL, jsonParam) | ||
if err != nil { | ||
return res, fmt.Errorf("error api request - %v - %s", err, string(rawRes)) | ||
} | ||
if err := json.Unmarshal(rawRes, &res); err != nil { | ||
return res, fmt.Errorf("can not parse body - %v", err) | ||
} | ||
return res, nil | ||
} |
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
Oops, something went wrong.