Skip to content

Commit ddf8c49

Browse files
spezaSkarlso
authored andcommitted
Fix basic Go Report Card issues (#167)
1 parent 41ccd5c commit ddf8c49

18 files changed

+72
-60
lines changed

auth/role.go

+25-24
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ package auth
22

33
import "github.com/gaia-pipeline/gaia"
44

5-
// Constructor for creating new UserRoleEndpoints.
5+
// NewUserRoleEndpoint is a constructor for creating new UserRoleEndpoints.
66
func NewUserRoleEndpoint(method string, path string) *gaia.UserRoleEndpoint {
77
return &gaia.UserRoleEndpoint{Path: path, Method: method}
88
}
99

10-
// Creates a full user role name {category}{role}.
10+
// FullUserRoleName returns a full user role name in the form {category}{role}.
1111
func FullUserRoleName(category *gaia.UserRoleCategory, role *gaia.UserRole) string {
1212
return category.Name + role.Name
1313
}
1414

15-
// Flattens the given user categories into a single slice of {category}{role}s.
15+
// FlattenUserCategoryRoles flattens the given user categories into a single slice with items in the form off
16+
// {category}{role}s.
1617
func FlattenUserCategoryRoles(cats []*gaia.UserRoleCategory) []string {
1718
var roles []string
1819
for _, category := range cats {
@@ -24,15 +25,15 @@ func FlattenUserCategoryRoles(cats []*gaia.UserRoleCategory) []string {
2425
}
2526

2627
var (
27-
// All the default user categories and roles.
28+
// DefaultUserRoles contains all the default user categories and roles.
2829
DefaultUserRoles = []*gaia.UserRoleCategory{
2930
{
3031
Name: "Pipeline",
3132
Description: "Managing and initiating pipelines.",
3233
Roles: []*gaia.UserRole{
3334
{
3435
Name: "Create",
35-
ApiEndpoint: []*gaia.UserRoleEndpoint{
36+
APIEndpoint: []*gaia.UserRoleEndpoint{
3637
NewUserRoleEndpoint("POST", "/api/v1/pipeline"),
3738
NewUserRoleEndpoint("POST", "/api/v1/pipeline/gitlsremote"),
3839
NewUserRoleEndpoint("GET", "/api/v1/pipeline/name"),
@@ -42,7 +43,7 @@ var (
4243
},
4344
{
4445
Name: "List",
45-
ApiEndpoint: []*gaia.UserRoleEndpoint{
46+
APIEndpoint: []*gaia.UserRoleEndpoint{
4647
NewUserRoleEndpoint("GET", "/api/v1/pipeline/created"),
4748
NewUserRoleEndpoint("GET", "/api/v1/pipeline"),
4849
NewUserRoleEndpoint("GET", "/api/v1/pipeline/latest"),
@@ -51,28 +52,28 @@ var (
5152
},
5253
{
5354
Name: "Get",
54-
ApiEndpoint: []*gaia.UserRoleEndpoint{
55+
APIEndpoint: []*gaia.UserRoleEndpoint{
5556
NewUserRoleEndpoint("GET", "/api/v1/pipeline/:pipelineid"),
5657
},
5758
Description: "Get created pipelines.",
5859
},
5960
{
6061
Name: "Update",
61-
ApiEndpoint: []*gaia.UserRoleEndpoint{
62+
APIEndpoint: []*gaia.UserRoleEndpoint{
6263
NewUserRoleEndpoint("PUT", "/api/v1/pipeline/:pipelineid"),
6364
},
6465
Description: "Update created pipelines.",
6566
},
6667
{
6768
Name: "Delete",
68-
ApiEndpoint: []*gaia.UserRoleEndpoint{
69+
APIEndpoint: []*gaia.UserRoleEndpoint{
6970
NewUserRoleEndpoint("DELETE", "/api/v1/pipeline/:pipelineid"),
7071
},
7172
Description: "Delete created pipelines.",
7273
},
7374
{
7475
Name: "Start",
75-
ApiEndpoint: []*gaia.UserRoleEndpoint{
76+
APIEndpoint: []*gaia.UserRoleEndpoint{
7677
NewUserRoleEndpoint("POST", "/api/v1/pipeline/:pipelineid/start"),
7778
},
7879
Description: "Start created pipelines.",
@@ -85,29 +86,29 @@ var (
8586
Roles: []*gaia.UserRole{
8687
{
8788
Name: "Stop",
88-
ApiEndpoint: []*gaia.UserRoleEndpoint{
89+
APIEndpoint: []*gaia.UserRoleEndpoint{
8990
NewUserRoleEndpoint("POST", "/api/v1/pipelinerun/:pipelineid/:runid/stop"),
9091
},
9192
Description: "Stop running pipelines.",
9293
},
9394
{
9495
Name: "Get",
95-
ApiEndpoint: []*gaia.UserRoleEndpoint{
96+
APIEndpoint: []*gaia.UserRoleEndpoint{
9697
NewUserRoleEndpoint("GET", "/api/v1/pipelinerun/:pipelineid/:runid"),
9798
NewUserRoleEndpoint("GET", "/api/v1/pipelinerun/:pipelineid/latest"),
9899
},
99100
Description: "Get pipeline runs.",
100101
},
101102
{
102103
Name: "List",
103-
ApiEndpoint: []*gaia.UserRoleEndpoint{
104+
APIEndpoint: []*gaia.UserRoleEndpoint{
104105
NewUserRoleEndpoint("GET", "pipelinerun/:pipelineid"),
105106
},
106107
Description: "List pipeline runs.",
107108
},
108109
{
109110
Name: "Logs",
110-
ApiEndpoint: []*gaia.UserRoleEndpoint{
111+
APIEndpoint: []*gaia.UserRoleEndpoint{
111112
NewUserRoleEndpoint("GET", "/api/v1/pipelinerun/:pipelineid/:runid/latest"),
112113
},
113114
Description: "Get logs for pipeline runs.",
@@ -120,28 +121,28 @@ var (
120121
Roles: []*gaia.UserRole{
121122
{
122123
Name: "List",
123-
ApiEndpoint: []*gaia.UserRoleEndpoint{
124+
APIEndpoint: []*gaia.UserRoleEndpoint{
124125
NewUserRoleEndpoint("GET", "/api/v1/secrets"),
125126
},
126127
Description: "List created secrets.",
127128
},
128129
{
129130
Name: "Delete",
130-
ApiEndpoint: []*gaia.UserRoleEndpoint{
131+
APIEndpoint: []*gaia.UserRoleEndpoint{
131132
NewUserRoleEndpoint("DELETE", "/api/v1/secret/:key"),
132133
},
133134
Description: "Delete created secrets.",
134135
},
135136
{
136137
Name: "Create",
137-
ApiEndpoint: []*gaia.UserRoleEndpoint{
138+
APIEndpoint: []*gaia.UserRoleEndpoint{
138139
NewUserRoleEndpoint("POST", "/api/v1/secret"),
139140
},
140141
Description: "Create new secrets.",
141142
},
142143
{
143144
Name: "Update",
144-
ApiEndpoint: []*gaia.UserRoleEndpoint{
145+
APIEndpoint: []*gaia.UserRoleEndpoint{
145146
NewUserRoleEndpoint("PUT", "/api/v1/secret/update"),
146147
},
147148
Description: "Update created secrets.",
@@ -154,28 +155,28 @@ var (
154155
Roles: []*gaia.UserRole{
155156
{
156157
Name: "Create",
157-
ApiEndpoint: []*gaia.UserRoleEndpoint{
158+
APIEndpoint: []*gaia.UserRoleEndpoint{
158159
NewUserRoleEndpoint("POST", "/api/v1/user"),
159160
},
160161
Description: "Create new users.",
161162
},
162163
{
163164
Name: "List",
164-
ApiEndpoint: []*gaia.UserRoleEndpoint{
165+
APIEndpoint: []*gaia.UserRoleEndpoint{
165166
NewUserRoleEndpoint("GET", "/api/v1/users"),
166167
},
167168
Description: "List created users.",
168169
},
169170
{
170171
Name: "ChangePassword",
171-
ApiEndpoint: []*gaia.UserRoleEndpoint{
172+
APIEndpoint: []*gaia.UserRoleEndpoint{
172173
NewUserRoleEndpoint("POST", "/api/v1/user/password"),
173174
},
174175
Description: "Change created users passwords.",
175176
},
176177
{
177178
Name: "Delete",
178-
ApiEndpoint: []*gaia.UserRoleEndpoint{
179+
APIEndpoint: []*gaia.UserRoleEndpoint{
179180
NewUserRoleEndpoint("DELETE", "/api/v1/user/:username"),
180181
},
181182
Description: "Delete created users.",
@@ -188,14 +189,14 @@ var (
188189
Roles: []*gaia.UserRole{
189190
{
190191
Name: "Get",
191-
ApiEndpoint: []*gaia.UserRoleEndpoint{
192+
APIEndpoint: []*gaia.UserRoleEndpoint{
192193
NewUserRoleEndpoint("GET", "/api/v1/user/:username/permissions"),
193194
},
194195
Description: "Get created users permissions.",
195196
},
196197
{
197198
Name: "Update",
198-
ApiEndpoint: []*gaia.UserRoleEndpoint{
199+
APIEndpoint: []*gaia.UserRoleEndpoint{
199200
NewUserRoleEndpoint("PUT", "/api/v1/user/:username/permissions"),
200201
},
201202
Description: "Update created users permissions.",

gaia.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ type User struct {
115115
LastLogin time.Time `json:"lastlogin,omitempty"`
116116
}
117117

118-
// User Permission is stored in its own data structure away from the core user. It represents all permission data
118+
// UserPermission is stored in its own data structure away from the core user. It represents all permission data
119119
// for a single user.
120120
type UserPermission struct {
121121
Username string `json:"username"`
@@ -134,7 +134,7 @@ type UserRoleCategory struct {
134134
type UserRole struct {
135135
Name string `json:"name"`
136136
Description string `json:"description"`
137-
ApiEndpoint []*UserRoleEndpoint `json:"api_endpoints"`
137+
APIEndpoint []*UserRoleEndpoint `json:"api_endpoints"`
138138
}
139139

140140
// UserRoleEndpoint represents the path and method of the API endpoint to be secured.

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ require (
2929
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 // indirect
3030
github.com/oklog/run v1.0.0 // indirect
3131
github.com/pelletier/go-buffruneio v0.2.0 // indirect
32-
github.com/pkg/errors v0.8.1 // indirect
32+
github.com/pkg/errors v0.8.1
3333
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967
3434
github.com/satori/go.uuid v1.2.0
3535
github.com/sergi/go-diff v1.0.0 // indirect

handlers/auth.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ var (
1818
errNotAuthorized = errors.New("no or invalid jwt token provided. You are not authorized")
1919
)
2020

21-
// Authentication middleware used for each request. Includes functionality that validates tokens and user permissions.
21+
// AuthMiddleware is middleware used for each request. Includes functionality that validates the JWT tokens and user
22+
// permissions.
2223
func AuthMiddleware(roleAuth *AuthConfig) echo.MiddlewareFunc {
2324
return func(next echo.HandlerFunc) echo.HandlerFunc {
2425
return func(c echo.Context) error {
@@ -56,8 +57,8 @@ func AuthMiddleware(roleAuth *AuthConfig) echo.MiddlewareFunc {
5657
}
5758
}
5859

59-
// Simple auth config struct to be passed into the AuthMiddleware. Currently allow the ability to specify the
60-
// permission roles applied for each echo request.
60+
// AuthConfig is a simple config struct to be passed into AuthMiddleware. Currently allows the ability to specify
61+
// the permission roles required for each echo endpoint.
6162
type AuthConfig struct {
6263
RoleCategories []*gaia.UserRoleCategory
6364
}
@@ -74,14 +75,14 @@ func (ra *AuthConfig) checkRole(userRoles interface{}, method, path string) erro
7475
return nil
7576
}
7677
}
77-
return errors.New(fmt.Sprintf("Required permission role %s", perm))
78+
return fmt.Errorf("Required permission role %s", perm)
7879
}
7980

8081
// Iterate over each category to find a permission (if existing) for this API endpoint.
8182
func (ra *AuthConfig) getRequiredRole(method, path string) string {
8283
for _, category := range ra.RoleCategories {
8384
for _, role := range category.Roles {
84-
for _, endpoint := range role.ApiEndpoint {
85+
for _, endpoint := range role.APIEndpoint {
8586
// If the http method & path match then return the role required for this endpoint
8687
if method == endpoint.Method && path == endpoint.Path {
8788
return auth.FullUserRoleName(category, role)

handlers/auth_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ var mockRoleAuth = &AuthConfig{
2424
Roles: []*gaia.UserRole{
2525
{
2626
Name: "GetSingle",
27-
ApiEndpoint: []*gaia.UserRoleEndpoint{
27+
APIEndpoint: []*gaia.UserRoleEndpoint{
2828
auth.NewUserRoleEndpoint("GET", "/catone/:id"),
2929
auth.NewUserRoleEndpoint("GET", "/catone/latest"),
3030
},
3131
},
3232
{
3333
Name: "PostSingle",
34-
ApiEndpoint: []*gaia.UserRoleEndpoint{
34+
APIEndpoint: []*gaia.UserRoleEndpoint{
3535
auth.NewUserRoleEndpoint("POST", "/catone"),
3636
},
3737
},
@@ -42,13 +42,13 @@ var mockRoleAuth = &AuthConfig{
4242
Roles: []*gaia.UserRole{
4343
{
4444
Name: "GetSingle",
45-
ApiEndpoint: []*gaia.UserRoleEndpoint{
45+
APIEndpoint: []*gaia.UserRoleEndpoint{
4646
auth.NewUserRoleEndpoint("GET", "/cattwo/:first/:second"),
4747
},
4848
},
4949
{
5050
Name: "PostSingle",
51-
ApiEndpoint: []*gaia.UserRoleEndpoint{
51+
APIEndpoint: []*gaia.UserRoleEndpoint{
5252
auth.NewUserRoleEndpoint("POST", "/cattwo/:first/:second/start"),
5353
},
5454
},

handlers/permission.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/labstack/echo"
88
)
99

10-
// Simply retrieves a list of all user role categories
10+
// PermissionGetAll simply returns a list of all the roles available.
1111
func PermissionGetAll(c echo.Context) error {
1212
return c.JSON(http.StatusOK, auth.DefaultUserRoles)
1313
}

handlers/user.go

+2
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func UserAdd(c echo.Context) error {
192192
return c.String(http.StatusCreated, "User has been added")
193193
}
194194

195+
// UserGetPermissions returns the permissions for a user.
195196
func UserGetPermissions(c echo.Context) error {
196197
u := c.Param("username")
197198
storeService, _ := services.StorageService()
@@ -202,6 +203,7 @@ func UserGetPermissions(c echo.Context) error {
202203
return c.JSON(http.StatusOK, perms)
203204
}
204205

206+
// UserPutPermissions adds or updates permissions for a user.
205207
func UserPutPermissions(c echo.Context) error {
206208
var perms *gaia.UserPermission
207209
if err := c.Bind(&perms); err != nil {

handlers/user_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestUserLoginHMACKey(t *testing.T) {
5959
user := &gaia.User{}
6060
err = json.Unmarshal(data, user)
6161
if err != nil {
62-
t.Fatalf("error unmarshaling responce %v", err.Error())
62+
t.Fatalf("error unmarshaling response %v", err.Error())
6363
}
6464
token, _, err := new(jwt.Parser).ParseUnverified(user.Tokenstring, jwt.MapClaims{})
6565
if err != nil {
@@ -112,7 +112,7 @@ func TestUserLoginRSAKey(t *testing.T) {
112112
user := &gaia.User{}
113113
err = json.Unmarshal(data, user)
114114
if err != nil {
115-
t.Fatalf("error unmarshaling responce %v", err.Error())
115+
t.Fatalf("error unmarshaling response %v", err.Error())
116116
}
117117
token, _, err := new(jwt.Parser).ParseUnverified(user.Tokenstring, jwt.MapClaims{})
118118
if err != nil {

plugin/plugin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func (p *Plugin) Execute(j *gaia.Job) error {
205205
timeString := time.Now().Format(timeFormat)
206206
p.writer.WriteString(fmt.Sprintf("%s Job '%s' threw an error: %s\n", timeString, j.Title, resultObj.Message))
207207
} else if err != nil {
208-
// An error occured during the send or somewhere else.
208+
// An error occurred during the send or somewhere else.
209209
// The job itself usually does not return an error here.
210210
// We mark the job as failed.
211211
j.Status = gaia.JobFailed

plugin/plugin_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ func TestGetJobs(t *testing.T) {
130130

131131
func TestRebuildDepTree(t *testing.T) {
132132
l := []gaia.Job{
133-
gaia.Job{ID: 12345},
134-
gaia.Job{ID: 1234},
135-
gaia.Job{ID: 123},
133+
{ID: 12345},
134+
{ID: 1234},
135+
{ID: 123},
136136
}
137137
dep := []uint32{1234, 123}
138138
depTree := rebuildDepTree(dep, l)

security/ca.go

+7
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,16 @@ func (c *CA) CreateSignedCert() (string, string, error) {
190190

191191
// Sign the certificate
192192
certSigned, err := x509.CreateCertificate(rand.Reader, cert, ca, pub, caPlain.PrivateKey)
193+
if err != nil {
194+
return "", "", err
195+
}
193196

194197
// Public key
195198
certOut, err := ioutil.TempFile("", "crt")
199+
if err != nil {
200+
return "", "", err
201+
}
202+
196203
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: certSigned})
197204
certOut.Close()
198205

0 commit comments

Comments
 (0)