From 109f72fcf97511b25be58253b2f44afd567d27d4 Mon Sep 17 00:00:00 2001 From: stonezdj Date: Thu, 12 Dec 2024 15:10:27 +0800 Subject: [PATCH 1/4] Remove unused file Signed-off-by: stonezdj --- src/common/utils/email/mail.go | 157 ---------------------------- src/common/utils/email/mail_test.go | 105 ------------------- 2 files changed, 262 deletions(-) delete mode 100644 src/common/utils/email/mail.go delete mode 100644 src/common/utils/email/mail_test.go diff --git a/src/common/utils/email/mail.go b/src/common/utils/email/mail.go deleted file mode 100644 index 8f49c254f0d..00000000000 --- a/src/common/utils/email/mail.go +++ /dev/null @@ -1,157 +0,0 @@ -// 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 email - -import ( - tlspkg "crypto/tls" - "fmt" - "net" - "net/smtp" - "strings" - "time" - - "github.com/goharbor/harbor/src/lib/log" -) - -// Send ... -func Send(addr, identity, username, password string, - timeout int, tls, insecure bool, from string, - to []string, subject, message string) error { - client, err := newClient(addr, identity, username, - password, timeout, tls, insecure) - if err != nil { - return err - } - defer client.Close() - - if err = client.Mail(from); err != nil { - return err - } - - for _, t := range to { - if err = client.Rcpt(t); err != nil { - return err - } - } - - w, err := client.Data() - if err != nil { - return err - } - - template := "From: %s\r\nTo: %s\r\nSubject: %s\r\nMIME-version: 1.0;\r\nContent-Type: text/html; charset=\"UTF-8\"\r\n\n%s\r\n" - data := fmt.Sprintf(template, from, - strings.Join(to, ","), subject, message) - - _, err = w.Write([]byte(data)) - if err != nil { - return err - } - - err = w.Close() - if err != nil { - return err - } - - return client.Quit() -} - -// Ping tests the connection and authentication with email server -// If tls is true, a secure connection is established, or Ping -// trys to upgrade the insecure connection to a secure one if -// email server supports it. -// Ping doesn't verify the server's certificate and hostname when -// needed if the parameter insecure is ture -func Ping(addr, identity, username, password string, - timeout int, tls, insecure bool) error { - client, err := newClient(addr, identity, username, password, - timeout, tls, insecure) - if err != nil { - return err - } - defer client.Close() - return nil -} - -// caller needs to close the client -func newClient(addr, identity, username, password string, - timeout int, tls, insecure bool) (*smtp.Client, error) { - log.Debugf("establishing TCP connection with %s ...", addr) - conn, err := net.DialTimeout("tcp", addr, - time.Duration(timeout)*time.Second) - if err != nil { - return nil, err - } - - host, _, err := net.SplitHostPort(addr) - if err != nil { - return nil, err - } - - if tls { - log.Debugf("establishing SSL/TLS connection with %s ...", addr) - tlsConn := tlspkg.Client(conn, &tlspkg.Config{ - ServerName: host, - InsecureSkipVerify: insecure, - }) - if err = tlsConn.Handshake(); err != nil { - return nil, err - } - - conn = tlsConn - } - - log.Debugf("creating SMTP client for %s ...", host) - client, err := smtp.NewClient(conn, host) - if err != nil { - return nil, err - } - - // try to switch to SSL/TLS - if !tls { - if ok, _ := client.Extension("STARTTLS"); ok { - log.Debugf("switching the connection with %s to SSL/TLS ...", addr) - if err = client.StartTLS(&tlspkg.Config{ - ServerName: host, - InsecureSkipVerify: insecure, - }); err != nil { - return nil, err - } - tls = true - } else { - log.Debugf("the email server %s does not support STARTTLS", addr) - } - } - - if ok, _ := client.Extension("AUTH"); ok { - log.Debug("authenticating the client...") - var auth smtp.Auth - if tls { - auth = smtp.PlainAuth(identity, username, password, host) - } else { - auth = smtp.CRAMMD5Auth(username, password) - } - if err = client.Auth(auth); err != nil { - return nil, err - } - } else { - log.Debugf("the email server %s does not support AUTH, skip", - addr) - } - - log.Debug("create smtp client successfully") - - return client, nil -} diff --git a/src/common/utils/email/mail_test.go b/src/common/utils/email/mail_test.go deleted file mode 100644 index 35e09b33504..00000000000 --- a/src/common/utils/email/mail_test.go +++ /dev/null @@ -1,105 +0,0 @@ -// 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 email - -import ( - "strings" - "testing" - // "github.com/stretchr/testify/assert" -) - -func TestSend(t *testing.T) { - addr := "smtp.gmail.com:465" - identity := "" - username := "harbortestonly@gmail.com" - password := "harborharbor" - timeout := 60 - tls := true - insecure := false - from := "from" - to := []string{username} - subject := "subject" - message := "message" - - // tls connection - tls = true - err := Send(addr, identity, username, password, - timeout, tls, insecure, from, to, - subject, message) - // bypass the check due to security policy change on gmail - // TODO - // assert.Nil(t, err) - - /*not work on ci - // non-tls connection - addr = "smtp.gmail.com:25" - tls = false - err = Send(addr, identity, username, password, - timeout, tls, insecure, from, to, - subject, message) - assert.Nil(t, err) - */ - - // invalid username/password - username = "invalid_username" - err = Send(addr, identity, username, password, - timeout, tls, insecure, from, to, - subject, message) - if err == nil { - t.Errorf("there should be an auth error") - } else { - if !strings.Contains(err.Error(), "535") { - t.Errorf("unexpected error: %v", err) - } - } -} - -func TestPing(t *testing.T) { - addr := "smtp.gmail.com:465" - identity := "" - username := "harbortestonly@gmail.com" - password := "harborharbor" - timeout := 0 - tls := true - insecure := false - - // tls connection - err := Ping(addr, identity, username, password, - timeout, tls, insecure) - // bypass the check due to security policy change on gmail - // TODO - // assert.Nil(t, err) - - /*not work on ci - // non-tls connection - addr = "smtp.gmail.com:25" - tls = false - err = Ping(addr, identity, username, password, - timeout, tls, insecure) - assert.Nil(t, err) - */ - - // invalid username/password - username = "invalid_username" - err = Ping(addr, identity, username, password, - timeout, tls, insecure) - if err == nil { - t.Errorf("there should be an auth error") - } else { - if !strings.Contains(err.Error(), "535") { - t.Errorf("unexpected error: %v", err) - } - } -} From dcedd079e2858c222dd55bdf7ce436bc377eaa11 Mon Sep 17 00:00:00 2001 From: stonezdj Date: Thu, 12 Dec 2024 15:15:14 +0800 Subject: [PATCH 2/4] Delete notary-migration-fix.sh Signed-off-by: stonezdj --- tools/notary-migration-fix.sh | 63 ----------------------------------- 1 file changed, 63 deletions(-) delete mode 100644 tools/notary-migration-fix.sh diff --git a/tools/notary-migration-fix.sh b/tools/notary-migration-fix.sh deleted file mode 100644 index c82785638a5..00000000000 --- a/tools/notary-migration-fix.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/bin/bash -# 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. -# - -set -ex - -harbor_db_image=$(docker images goharbor/harbor-db --format "{{.Repository}}:{{.Tag}}") -harbor_db_path="/data/database" - -launch_db() { - docker run -d --name fix-notary-migration -v ${harbor_db_path}:/var/lib/postgresql/data ${harbor_db_image} "postgres" -} - -clean_db() { - docker stop fix-notary-migration - docker rm fix-notary-migration -} - -wait_for_db_ready() { - set +e - TIMEOUT=12 - while [ $TIMEOUT -gt 0 ]; do - docker exec fix-notary-migration pg_isready | grep "accepting connections" - if [ $? -eq 0 ]; then - break - fi - TIMEOUT=$((TIMEOUT - 1)) - sleep 5 - done - if [ $TIMEOUT -eq 0 ]; then - echo "Harbor DB cannot reach within one minute." - clean_db - exit 1 - fi - set -e -} - -fix_notary_server() { - docker exec fix-notary-migration psql -U postgres -d notaryserver -c "delete from schema_migrations where version > 2 and not exists(select column_name from information_schema.columns where table_name = 'schema_migrations' and column_name = 'dirty');" -} - -fix_notary_signer() { - docker exec fix-notary-migration psql -U postgres -d notarysigner -c "delete from schema_migrations where version > 1 and not exists(select column_name from information_schema.columns where table_name = 'schema_migrations' and column_name = 'dirty');" -} - - -launch_db -wait_for_db_ready -fix_notary_server -fix_notary_signer -clean_db \ No newline at end of file From a51f84927e96707f7dca8049b68db8bc42ded15e Mon Sep 17 00:00:00 2001 From: stonezdj Date: Thu, 12 Dec 2024 15:25:15 +0800 Subject: [PATCH 3/4] remove unused file Signed-off-by: stonezdj --- src/common/dao/utils.go | 25 ------------------------- src/common/dao/utils_test.go | 24 ------------------------ 2 files changed, 49 deletions(-) delete mode 100644 src/common/dao/utils.go delete mode 100644 src/common/dao/utils_test.go diff --git a/src/common/dao/utils.go b/src/common/dao/utils.go deleted file mode 100644 index e001dfb4072..00000000000 --- a/src/common/dao/utils.go +++ /dev/null @@ -1,25 +0,0 @@ -// 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 dao - -import ( - "fmt" - "strings" -) - -// JoinNumberConditions - To join number condition into string,used in sql query -func JoinNumberConditions(ids []int) string { - return strings.Trim(strings.Replace(fmt.Sprint(ids), " ", ",", -1), "[]") -} diff --git a/src/common/dao/utils_test.go b/src/common/dao/utils_test.go deleted file mode 100644 index 78f2f4a3a77..00000000000 --- a/src/common/dao/utils_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package dao - -import "testing" - -func TestJoinNumberConditions(t *testing.T) { - type args struct { - ids []int - } - tests := []struct { - name string - args args - want string - }{ - {name: "normal test", args: args{[]int{1, 2, 3}}, want: "1,2,3"}, - {name: "dummy test", args: args{[]int{}}, want: ""}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := JoinNumberConditions(tt.args.ids); got != tt.want { - t.Errorf("JoinNumberConditions() = %v, want %v", got, tt.want) - } - }) - } -} From 9daa81b1b23c31a87a8b6335fa478b96218e5727 Mon Sep 17 00:00:00 2001 From: stonezdj Date: Thu, 12 Dec 2024 15:49:03 +0800 Subject: [PATCH 4/4] remove unused files Signed-off-by: stonezdj --- src/common/const.go | 7 ------ src/common/job/const.go | 50 ----------------------------------------- 2 files changed, 57 deletions(-) delete mode 100644 src/common/job/const.go diff --git a/src/common/const.go b/src/common/const.go index a8166cea3a2..c0fef4997c0 100644 --- a/src/common/const.go +++ b/src/common/const.go @@ -40,15 +40,10 @@ const ( RoleMaintainer = 4 RoleLimitedGuest = 5 - LabelLevelSystem = "s" LabelLevelUser = "u" LabelScopeGlobal = "g" LabelScopeProject = "p" - ResourceTypeProject = "p" - ResourceTypeRepository = "r" - ResourceTypeImage = "i" - ExtEndpoint = "ext_endpoint" AUTHMode = "auth_mode" PrimaryAuthMode = "primary_auth_mode" @@ -120,7 +115,6 @@ const ( OIDCScope = "oidc_scope" OIDCUserClaim = "oidc_user_claim" - CfgDriverDB = "db" NewHarborAdminName = "admin@harbor.local" RegistryStorageProviderName = "registry_storage_provider_name" RegistryControllerURL = "registry_controller_url" @@ -128,7 +122,6 @@ const ( GroupMember = "g" ReadOnly = "read_only" TrivyAdapterURL = "trivy_adapter_url" - DefaultCoreEndpoint = "http://core:8080" LDAPGroupType = 1 HTTPGroupType = 2 OIDCGroupType = 3 diff --git a/src/common/job/const.go b/src/common/job/const.go deleted file mode 100644 index f580998d024..00000000000 --- a/src/common/job/const.go +++ /dev/null @@ -1,50 +0,0 @@ -// 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 job - -const ( - // ImageScanJob is name of scan job it will be used as key to register to job service. - ImageScanJob = "IMAGE_SCAN" - // ImageScanAllJob is the name of "scanall" job in job service - ImageScanAllJob = "IMAGE_SCAN_ALL" - // ImageGC the name of image garbage collection job in job service - ImageGC = "IMAGE_GC" - // ImageGCReadOnly the name of image garbage collection read only job in job service - ImageGCReadOnly = "IMAGE_GC_READ_ONLY" - // JobKindGeneric : Kind of generic job - JobKindGeneric = "Generic" - // JobKindScheduled : Kind of scheduled job - JobKindScheduled = "Scheduled" - // JobKindPeriodic : Kind of periodic job - JobKindPeriodic = "Periodic" - - // JobServiceStatusPending : job status pending - JobServiceStatusPending = "Pending" - // JobServiceStatusRunning : job status running - JobServiceStatusRunning = "Running" - // JobServiceStatusStopped : job status stopped - JobServiceStatusStopped = "Stopped" - // JobServiceStatusCancelled : job status cancelled - JobServiceStatusCancelled = "Cancelled" - // JobServiceStatusError : job status error - JobServiceStatusError = "Error" - // JobServiceStatusSuccess : job status success - JobServiceStatusSuccess = "Success" - // JobServiceStatusScheduled : job status scheduled - JobServiceStatusScheduled = "Scheduled" - - // JobActionStop : the action to stop the job - JobActionStop = "stop" -)