Skip to content

Commit

Permalink
Merge pull request #362 from ARGOeu/devel
Browse files Browse the repository at this point in the history
Version 1.1.0
  • Loading branch information
themiszamani authored Nov 8, 2021
2 parents 301e766 + 72b5075 commit 36de75c
Show file tree
Hide file tree
Showing 32 changed files with 953 additions and 138 deletions.
19 changes: 15 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,22 @@ func (cfg *APICfg) GetZooList() ([]string, error) {
// LoadCAs builds the CA chain using pem files from the specified directory in the cfg
func (cfg *APICfg) LoadCAs() (roots *x509.CertPool) {

log.Info("Building the root CA chain...")
log.WithFields(
log.Fields{
"type": "service_log",
},
).Info("building the root CA chain...")

pattern := "*.pem"
roots = x509.NewCertPool()

err := filepath.Walk(cfg.CertificateAuthoritiesDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Errorf("Prevent panic by handling failure accessing a path %q: %v\n", cfg.CertificateAuthoritiesDir, err)
log.WithFields(
log.Fields{
"type": "service_log",
},
).Errorf("Prevent panic by handling failure accessing a path %q: %v", cfg.CertificateAuthoritiesDir, err)
return err
}

Expand All @@ -224,8 +232,11 @@ func (cfg *APICfg) LoadCAs() (roots *x509.CertPool) {
log.Errorf("error walking the path %q: %v\n", cfg.CertificateAuthoritiesDir, err)
}

log.Info("All certificates parsed successfully.")

log.WithFields(
log.Fields{
"type": "service_log",
},
).Info("All certificates parsed successfully")
return roots
}

Expand Down
20 changes: 13 additions & 7 deletions doc/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2456,27 +2456,27 @@ definitions:
properties:
projects_metrics:
type: object
$ref: '#/definitions/TotalProjectMessageCount'
users_count:
$ref: '#/definitions/TotalProjectMetrics'
total_users_count:
type: integer
topics_count:
total_topics_count:
type: integer
subscriptions_count:
total_subscriptions_count:
type: integer

TotalProjectMessageCount:
TotalProjectMetrics:
type: object
properties:
projects:
type: array
items:
$ref: '#/definitions/ProjectMessageCount'
$ref: '#/definitions/ProjectMetrics'
total_message_count:
type: integer
average_daily_messages:
type: integer

ProjectMessageCount:
ProjectMetrics:
type: object
properties:
project:
Expand All @@ -2485,6 +2485,12 @@ definitions:
type: integer
average_daily_messages:
type: integer
topics_count:
type: integer
subscriptions_count:
type: integer
users_count:
type: integer

Metrics:
type: object
Expand Down
16 changes: 11 additions & 5 deletions doc/v1/docs/api_metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,26 @@ Success Response
{
"project": "ARGO-2",
"message_count": 8,
"average_daily_messages": 2
"average_daily_messages": 2,
"users_count": 40,
"topics_count": 30,
"subscriptions_count": 100
},
{
"project": "ARGO",
"message_count": 25669,
"average_daily_messages": 120
"average_daily_messages": 120,
"users_count": 4,
"topics_count": 3,
"subscriptions_count": 0
}
],
"total_message_count": 25677,
"average_daily_messages": 122
},
"users_count": 44,
"topics_count": 33,
"subscriptions_counter": 100
"total_users_count": 44,
"total_topics_count": 33,
"total_subscriptions_count": 100
}
```
### Errors
Expand Down
3 changes: 3 additions & 0 deletions doc/v1/docs/api_users.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,9 @@ Please refer to section [Errors](api_errors.md) to see all possible Errors
This request lists information about a specific user
based on the authentication key provided as a url parameter

Authentication key can also take place in the
`x-api-key` header.

### Request
```
GET "/v1/users/profile"
Expand Down
12 changes: 9 additions & 3 deletions handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func WrapMockAuthConfig(hfn http.HandlerFunc, cfg *config.APICfg, brk brokers.Br
gorillaContext.Set(r, "str", nStr)
gorillaContext.Set(r, "mgr", mgr)
gorillaContext.Set(r, "apsc", c)
gorillaContext.Set(r, "authOption", cfg.AuthOption())
gorillaContext.Set(r, "auth_resource", cfg.ResAuth)
gorillaContext.Set(r, "auth_user", "UserA")
gorillaContext.Set(r, "auth_user_uuid", "uuid1")
Expand All @@ -88,6 +89,7 @@ func WrapConfig(hfn http.HandlerFunc, cfg *config.APICfg, brk brokers.Broker, st
gorillaContext.Set(r, "str", nStr)
gorillaContext.Set(r, "mgr", mgr)
gorillaContext.Set(r, "apsc", c)
gorillaContext.Set(r, "authOption", cfg.AuthOption())
gorillaContext.Set(r, "auth_resource", cfg.ResAuth)
gorillaContext.Set(r, "auth_service_token", cfg.ServiceToken)
gorillaContext.Set(r, "push_worker_token", cfg.PushWorkerToken)
Expand Down Expand Up @@ -305,10 +307,14 @@ func respondOK(w http.ResponseWriter, output []byte) {

// respondErr is used to finalize response writer with proper error codes and error output
func respondErr(w http.ResponseWriter, apiErr APIErrorRoot) {
log.Error(apiErr.Body.Code, "\t", apiErr.Body.Message)
// set the response code
log.WithFields(
log.Fields{
"type": "service_log",
"status_code": apiErr.Body.Code,
},
).Info(apiErr.Body.Message)

w.WriteHeader(apiErr.Body.Code)
// Output API Erorr object to JSON
output, _ := json.MarshalIndent(apiErr, "", " ")
w.Write(output)
}
Expand Down
2 changes: 1 addition & 1 deletion handlers/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func VaMetrics(w http.ResponseWriter, r *http.Request) {
startDate = time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
}

// if no end date was provided, set it to to today
// if no end date was provided, set it to today
if r.URL.Query().Get("end_date") != "" {
endDate, err = time.Parse("2006-01-02", r.URL.Query().Get("end_date"))
if err != nil {
Expand Down
22 changes: 14 additions & 8 deletions handlers/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,18 @@ func (suite *MetricsHandlersTestSuite) TestProjectMessageCount() {
{
"project": "ARGO",
"message_count": 30,
"average_daily_messages": 7
"average_daily_messages": 7,
"topics_count": 0,
"subscriptions_count": 0,
"users_count": 0
}
],
"total_message_count": 30,
"average_daily_messages": 7
},
"users_count": 0,
"topics_count": 0,
"subscriptions_count": 0
"total_users_count": 0,
"total_topics_count": 0,
"total_subscriptions_count": 0
}`

cfgKafka := config.NewAPICfg()
Expand Down Expand Up @@ -88,15 +91,18 @@ func (suite *MetricsHandlersTestSuite) TestVaReportFull() {
{
"project": "ARGO",
"message_count": 140,
"average_daily_messages": 0
"average_daily_messages": 0,
"topics_count": 4,
"subscriptions_count": 4,
"users_count": 7
}
],
"total_message_count": 140,
"average_daily_messages": 0
},
"users_count": 9,
"topics_count": 4,
"subscriptions_count": 4
"total_users_count": 7,
"total_topics_count": 4,
"total_subscriptions_count": 4
}`

cfgKafka := config.NewAPICfg()
Expand Down
10 changes: 6 additions & 4 deletions handlers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"fmt"
"github.com/ARGOeu/argo-messaging/auth"
"github.com/ARGOeu/argo-messaging/config"
"github.com/ARGOeu/argo-messaging/projects"
"github.com/ARGOeu/argo-messaging/stores"
gorillaContext "github.com/gorilla/context"
Expand All @@ -26,17 +27,18 @@ func UserProfile(w http.ResponseWriter, r *http.Request) {

// Grab context references
refStr := gorillaContext.Get(r, "str").(stores.Store)
authOption := gorillaContext.Get(r, "authOption").(config.AuthOption)

urlValues := r.URL.Query()
tokenExtractStrategy := GetRequestTokenExtractStrategy(authOption)
token := tokenExtractStrategy(r)

// if the url parameter 'key' is empty or absent, end the request with an unauthorized response
if urlValues.Get("key") == "" {
if token == "" {
err := APIErrorUnauthorized()
respondErr(w, err)
return
}

result, err := auth.GetUserByToken(urlValues.Get("key"), refStr)
result, err := auth.GetUserByToken(token, refStr)

if err != nil {
if err.Error() == "not found" {
Expand Down
60 changes: 59 additions & 1 deletion handlers/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (suite *UsersHandlersTestSuite) SetupTest() {
}`
}

func (suite *UsersHandlersTestSuite) TestUserProfile() {
func (suite *UsersHandlersTestSuite) TestUserProfileUrlKey() {

req, err := http.NewRequest("GET", "http://localhost:8080/v1/users/profile?key=S3CR3T1", nil)
if err != nil {
Expand Down Expand Up @@ -90,6 +90,64 @@ func (suite *UsersHandlersTestSuite) TestUserProfile() {
suite.Equal(expResp, w.Body.String())
}

func (suite *UsersHandlersTestSuite) TestUserProfileHeaderKey() {

req, err := http.NewRequest("GET", "http://localhost:8080/v1/users/profile", nil)
if err != nil {
log.Fatal(err)
}

req.Header.Add("x-api-key", "S3CR3T1")

expResp := `{
"uuid": "uuid1",
"projects": [
{
"project": "ARGO",
"roles": [
"consumer",
"publisher"
],
"topics": [
"topic1",
"topic2"
],
"subscriptions": [
"sub1",
"sub2",
"sub3"
]
}
],
"name": "UserA",
"first_name": "FirstA",
"last_name": "LastA",
"organization": "OrgA",
"description": "DescA",
"token": "S3CR3T1",
"email": "foo-email",
"service_roles": [],
"created_on": "2009-11-10T23:00:00Z",
"modified_on": "2009-11-10T23:00:00Z"
}`

cfgKafka := config.NewAPICfg()
cfgStr := `{
"auth_option":"header"
}`
cfgKafka.LoadStrJSON(cfgStr)

brk := brokers.MockBroker{}
str := stores.NewMockStore("whatever", "argo_mgs")
router := mux.NewRouter().StrictSlash(true)
w := httptest.NewRecorder()
mgr := oldPush.Manager{}
router.HandleFunc("/v1/users/profile", WrapMockAuthConfig(UserProfile, cfgKafka, &brk, str, &mgr, nil))
router.ServeHTTP(w, req)
suite.Equal(200, w.Code)
suite.Equal(expResp, w.Body.String())
}

func (suite *UsersHandlersTestSuite) TestUserProfileUnauthorized() {

req, err := http.NewRequest("GET", "http://localhost:8080/v1/users/profile?key=unknonwn", nil)
Expand Down
Loading

0 comments on commit 36de75c

Please sign in to comment.