-
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.
Feature: GDPR compliant audit logs (#17396)
- Loading branch information
Showing
17 changed files
with
274 additions
and
18 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,85 @@ | ||
// 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 gdpr | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/goharbor/harbor/src/jobservice/job" | ||
"github.com/goharbor/harbor/src/lib/errors" | ||
"github.com/goharbor/harbor/src/pkg/audit" | ||
"github.com/goharbor/harbor/src/pkg/user" | ||
) | ||
|
||
const UserNameParam = "username" | ||
|
||
type AuditLogsDataMasking struct { | ||
manager audit.Manager | ||
userManager user.Manager | ||
} | ||
|
||
func (a AuditLogsDataMasking) MaxFails() uint { | ||
return 3 | ||
} | ||
|
||
func (a AuditLogsDataMasking) MaxCurrency() uint { | ||
return 1 | ||
} | ||
|
||
func (a AuditLogsDataMasking) ShouldRetry() bool { | ||
return true | ||
} | ||
|
||
func (a AuditLogsDataMasking) Validate(params job.Parameters) error { | ||
if params == nil { | ||
// Params are required | ||
return errors.New("missing job parameters") | ||
} | ||
_, err := a.parseParams(params) | ||
return err | ||
} | ||
|
||
func (a *AuditLogsDataMasking) init() { | ||
if a.manager == nil { | ||
a.manager = audit.New() | ||
} | ||
if a.userManager == nil { | ||
a.userManager = user.New() | ||
} | ||
} | ||
|
||
func (a AuditLogsDataMasking) Run(ctx job.Context, params job.Parameters) error { | ||
logger := ctx.GetLogger() | ||
logger.Info("GDPR audit logs data masking job started") | ||
a.init() | ||
username, err := a.parseParams(params) | ||
if err != nil { | ||
return err | ||
} | ||
logger.Infof("Masking log entries for a user: %s", username) | ||
return a.manager.UpdateUsername(ctx.SystemContext(), username, a.userManager.GenerateCheckSum(username)) | ||
} | ||
|
||
func (a AuditLogsDataMasking) parseParams(params job.Parameters) (string, error) { | ||
value, exist := params[UserNameParam] | ||
if !exist { | ||
return "", fmt.Errorf("param %s not found", UserNameParam) | ||
} | ||
str, ok := value.(string) | ||
if !ok { | ||
return "", fmt.Errorf("the value of %s isn't string", UserNameParam) | ||
} | ||
return str, nil | ||
} |
67 changes: 67 additions & 0 deletions
67
src/jobservice/job/impl/gdpr/audit_logs_data_masking_test.go
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,67 @@ | ||
// 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 gdpr | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/goharbor/harbor/src/jobservice/job" | ||
mockjobservice "github.com/goharbor/harbor/src/testing/jobservice" | ||
"github.com/goharbor/harbor/src/testing/pkg/audit" | ||
"github.com/goharbor/harbor/src/testing/pkg/user" | ||
) | ||
|
||
func TestAuditLogsCleanupJobShouldRetry(t *testing.T) { | ||
rep := &AuditLogsDataMasking{} | ||
assert.True(t, rep.ShouldRetry()) | ||
} | ||
|
||
func TestAuditLogsCleanupJobValidateParams(t *testing.T) { | ||
const validUsername = "user" | ||
var ( | ||
manager = &audit.Manager{} | ||
userManager = &user.Manager{} | ||
) | ||
|
||
rep := &AuditLogsDataMasking{ | ||
manager: manager, | ||
userManager: userManager, | ||
} | ||
err := rep.Validate(nil) | ||
// parameters are required | ||
assert.Error(t, err) | ||
err = rep.Validate(job.Parameters{}) | ||
// no required username parameter | ||
assert.Error(t, err) | ||
validParams := job.Parameters{ | ||
"username": "user", | ||
} | ||
err = rep.Validate(validParams) | ||
// parameters are valid | ||
assert.Nil(t, err) | ||
|
||
ctx := &mockjobservice.MockJobContext{} | ||
logger := &mockjobservice.MockJobLogger{} | ||
|
||
ctx.On("GetLogger").Return(logger) | ||
userManager.On("GenerateCheckSum", validUsername).Return("hash") | ||
manager.On("UpdateUsername", context.TODO(), validUsername, "hash").Return(nil) | ||
|
||
err = rep.Run(ctx, validParams) | ||
assert.Nil(t, err) | ||
} |
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 |
---|---|---|
|
@@ -65,9 +65,9 @@ func (m *mgrTestSuite) TestUserDeleteGDPR() { | |
m.dao.On("Update", mock.Anything, testifymock.MatchedBy( | ||
func(u *models.User) bool { | ||
return u.UserID == 123 && | ||
u.Email == fmt.Sprintf("%s#%d", checkSum("[email protected]"), existingUser.UserID) && | ||
u.Username == fmt.Sprintf("%s#%d", checkSum("existing"), existingUser.UserID) && | ||
u.Realname == fmt.Sprintf("%s#%d", checkSum("RealName"), existingUser.UserID) && | ||
u.Email == fmt.Sprintf("%s#%d", m.mgr.GenerateCheckSum("[email protected]"), existingUser.UserID) && | ||
u.Username == fmt.Sprintf("%s#%d", m.mgr.GenerateCheckSum("existing"), existingUser.UserID) && | ||
u.Realname == fmt.Sprintf("%s#%d", m.mgr.GenerateCheckSum("RealName"), existingUser.UserID) && | ||
u.Deleted == true | ||
}), | ||
"username", | ||
|
Oops, something went wrong.