-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add user login event to audit log
Add common event handler Register login event Update previous audit log event redirect to auditlogext table Signed-off-by: stonezdj <[email protected]>
Showing
11 changed files
with
364 additions
and
99 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
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,94 @@ | ||
// Copyright Project Harbor Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package login | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"regexp" | ||
"time" | ||
|
||
"github.com/goharbor/harbor/src/common/rbac" | ||
event2 "github.com/goharbor/harbor/src/controller/event" | ||
"github.com/goharbor/harbor/src/controller/event/metadata/commonevent" | ||
"github.com/goharbor/harbor/src/controller/event/model" | ||
"github.com/goharbor/harbor/src/lib/config" | ||
"github.com/goharbor/harbor/src/pkg/notifier/event" | ||
) | ||
|
||
func init() { | ||
var loginLogoutResolver = &resolver{} | ||
commonevent.RegisterResolver(`/c/login$`, loginLogoutResolver) | ||
commonevent.RegisterResolver(`/c/log_out$`, loginLogoutResolver) | ||
} | ||
|
||
const ( | ||
opLogout = "logout" | ||
opLogin = "login" | ||
logoutSuffix = "log_out" | ||
payloadPattern = `principal=(.*?)&password` | ||
) | ||
|
||
type resolver struct { | ||
} | ||
|
||
func (l *resolver) Resolve(ce *commonevent.Metadata, event *event.Event) error { | ||
e := &model.CommonEvent{ | ||
Operator: ce.Username, | ||
ResourceType: rbac.ResourceUser.String(), | ||
ResourceName: ce.Username, | ||
OcurrAt: time.Now(), | ||
} | ||
|
||
// method POST for login, method GET for logout | ||
if ce.RequestMethod == http.MethodGet { | ||
e.Operation = opLogout | ||
e.OperationDescription = opLogout | ||
} else { | ||
e.Operation = opLogin | ||
e.OperationDescription = opLogin | ||
// Extract the username from payload | ||
re := regexp.MustCompile(payloadPattern) | ||
if len(ce.RequestPayload) > 0 { | ||
match := re.FindStringSubmatch(ce.RequestPayload) | ||
if len(match) > 1 { | ||
e.ResourceName = match[1] | ||
e.Operator = match[1] | ||
} | ||
} | ||
} | ||
e.IsSuccessful = true | ||
if ce.ResponseCode != http.StatusOK { | ||
e.IsSuccessful = false | ||
} | ||
event.Topic = event2.TopicCommonEvent | ||
event.Data = e | ||
return nil | ||
} | ||
|
||
func (e *resolver) PreCheck(ctx context.Context, _ string, method string) (bool, string) { | ||
operation := "" | ||
switch method { | ||
case http.MethodPost: | ||
operation = opLogin | ||
case http.MethodGet: | ||
operation = opLogout | ||
} | ||
if len(operation) == 0 { | ||
return false, "" | ||
} | ||
return config.AuditLogEventEnabled(ctx, fmt.Sprintf("%v_%v", operation, rbac.ResourceUser.String())), "" | ||
} |
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,105 @@ | ||
package login | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/goharbor/harbor/src/controller/event/metadata/commonevent" | ||
"github.com/goharbor/harbor/src/controller/event/model" | ||
"github.com/goharbor/harbor/src/pkg/notifier/event" | ||
) | ||
|
||
func Test_resolver_Resolve(t *testing.T) { | ||
type args struct { | ||
ce *commonevent.Metadata | ||
event *event.Event | ||
} | ||
tests := []struct { | ||
name string | ||
l *resolver | ||
args args | ||
wantErr bool | ||
wantUsername string | ||
wantOperation string | ||
wantOperationDescription string | ||
wantIsSuccessful bool | ||
}{ | ||
|
||
{"test normal", &resolver{}, args{ | ||
ce: &commonevent.Metadata{ | ||
Username: "test", | ||
RequestURL: "/c/login", | ||
RequestMethod: "POST", | ||
Payload: "principal=test&password=123456", | ||
ResponseCode: 200, | ||
}, event: &event.Event{}}, false, "test", "login", "login", true}, | ||
{"test fail", &resolver{}, args{ | ||
ce: &commonevent.Metadata{ | ||
Username: "test", | ||
RequestURL: "/c/login", | ||
RequestMethod: "POST", | ||
Payload: "principal=test&password=123456", | ||
ResponseCode: 401, | ||
}, event: &event.Event{}}, false, "test", "login", "login", false}, | ||
{"test logout", &resolver{}, args{ | ||
ce: &commonevent.Metadata{ | ||
Username: "test", | ||
RequestURL: "/c/log_out", | ||
RequestMethod: "GET", | ||
Payload: "", | ||
ResponseCode: 200, | ||
}, event: &event.Event{}}, false, "test", "logout", "logout", true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
l := &resolver{} | ||
if err := l.Resolve(tt.args.ce, tt.args.event); (err != nil) != tt.wantErr { | ||
t.Errorf("resolver.Resolve() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
if tt.args.event.Data.(*model.CommonEvent).Operator != tt.wantUsername { | ||
t.Errorf("resolver.Resolve() got = %v, want %v", tt.args.event.Data.(*model.CommonEvent).Operator, tt.wantUsername) | ||
} | ||
if tt.args.event.Data.(*model.CommonEvent).Operation != tt.wantOperation { | ||
t.Errorf("resolver.Resolve() got = %v, want %v", tt.args.event.Data.(*model.CommonEvent).Operation, tt.wantOperation) | ||
} | ||
if tt.args.event.Data.(*model.CommonEvent).OperationDescription != tt.wantOperationDescription { | ||
t.Errorf("resolver.Resolve() got = %v, want %v", tt.args.event.Data.(*model.CommonEvent).OperationDescription, tt.wantOperationDescription) | ||
} | ||
if tt.args.event.Data.(*model.CommonEvent).IsSuccessful != tt.wantIsSuccessful { | ||
t.Errorf("resolver.Resolve() got = %v, want %v", tt.args.event.Data.(*model.CommonEvent).IsSuccessful, tt.wantIsSuccessful) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_resolver_PreCheck(t *testing.T) { | ||
type args struct { | ||
ctx context.Context | ||
url string | ||
method string | ||
} | ||
tests := []struct { | ||
name string | ||
e *resolver | ||
args args | ||
wantMatched bool | ||
wantResourceName string | ||
}{ | ||
{"test normal", &resolver{}, args{context.Background(), "/c/login", "POST"}, true, ""}, | ||
{"test fail", &resolver{}, args{context.Background(), "/c/logout", "GET"}, true, ""}, | ||
{"test fail method", &resolver{}, args{context.Background(), "/c/login", "PUT"}, false, ""}, | ||
{"test fail wrong url", &resolver{}, args{context.Background(), "/c/logout", "DELETE"}, false, ""}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
e := &resolver{} | ||
got, got1 := e.PreCheck(tt.args.ctx, tt.args.url, tt.args.method) | ||
if got != tt.wantMatched { | ||
t.Errorf("resolver.PreCheck() got = %v, want %v", got, tt.wantMatched) | ||
} | ||
if got1 != tt.wantResourceName { | ||
t.Errorf("resolver.PreCheck() got1 = %v, want %v", got1, tt.wantResourceName) | ||
} | ||
}) | ||
} | ||
} |
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