Skip to content

Commit 038cfb1

Browse files
authored
Revise inernal references in pkg components
The location of these functions caused a cross internal/pkg reference, which made external SDK builds difficult. This removes the internal components from the pkg components to ensure proper pkg compatibility.
1 parent 9e6a3a0 commit 038cfb1

File tree

8 files changed

+75
-108
lines changed

8 files changed

+75
-108
lines changed

cmd/pgo/cmd/user.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/crunchydata/postgres-operator/cmd/pgo/api"
2424
"github.com/crunchydata/postgres-operator/cmd/pgo/util"
25+
"github.com/crunchydata/postgres-operator/internal/apiserver"
2526
utiloperator "github.com/crunchydata/postgres-operator/internal/util"
2627
msgs "github.com/crunchydata/postgres-operator/pkg/apiservermsgs"
2728

@@ -89,7 +90,7 @@ func createUser(args []string, ns string) {
8990
}
9091

9192
// determine if the user provies a valid password type
92-
if _, err := msgs.GetPasswordType(PasswordType); err != nil {
93+
if _, err := apiserver.GetPasswordType(PasswordType); err != nil {
9394
fmt.Println("Error:", err.Error())
9495
os.Exit(1)
9596
}
@@ -400,7 +401,7 @@ func updateUser(clusterNames []string, namespace string) {
400401
}
401402

402403
// determine if the user provies a valid password type
403-
if _, err := msgs.GetPasswordType(PasswordType); err != nil {
404+
if _, err := apiserver.GetPasswordType(PasswordType); err != nil {
404405
fmt.Println("Error:", err.Error())
405406
os.Exit(1)
406407
}

internal/apiserver/common.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"strings"
2323

24+
pgpassword "github.com/crunchydata/postgres-operator/internal/postgres/password"
2425
crv1 "github.com/crunchydata/postgres-operator/pkg/apis/crunchydata.com/v1"
2526

2627
log "github.com/sirupsen/logrus"
@@ -44,6 +45,9 @@ var (
4445
// ErrDBContainerNotFound is an error that indicates that a "database" container
4546
// could not be found in a specific pod
4647
ErrDBContainerNotFound = errors.New("\"database\" container not found in pod")
48+
// ErrPasswordTypeInvalid is used when a string that's not included in
49+
// PasswordTypeStrings is used
50+
ErrPasswordTypeInvalid = errors.New("invalid password type. choices are (md5, scram-sha-256)")
4751
// ErrStandbyNotAllowed contains the error message returned when an API call is not
4852
// permitted because it involves a cluster that is in standby mode
4953
ErrStandbyNotAllowed = errors.New("Action not permitted because standby mode is enabled")
@@ -54,6 +58,27 @@ var (
5458
"Operator installation")
5559
)
5660

61+
// passwordTypeStrings is a mapping of strings of password types to their
62+
// corresponding value of the structured password type
63+
var passwordTypeStrings = map[string]pgpassword.PasswordType{
64+
"": pgpassword.MD5,
65+
"md5": pgpassword.MD5,
66+
"scram": pgpassword.SCRAM,
67+
"scram-sha-256": pgpassword.SCRAM,
68+
}
69+
70+
// GetPasswordType returns the enumerated password type based on the string, and
71+
// an error if it cannot match one
72+
func GetPasswordType(passwordTypeStr string) (pgpassword.PasswordType, error) {
73+
passwordType, ok := passwordTypeStrings[passwordTypeStr]
74+
75+
if !ok {
76+
return passwordType, ErrPasswordTypeInvalid
77+
}
78+
79+
return passwordType, nil
80+
}
81+
5782
// IsValidPVC determines if a PVC with the name provided exits
5883
func IsValidPVC(pvcName, ns string) bool {
5984
ctx := context.TODO()

internal/apiserver/common_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,55 @@ limitations under the License.
1616
*/
1717

1818
import (
19+
"errors"
1920
"testing"
2021

22+
pgpassword "github.com/crunchydata/postgres-operator/internal/postgres/password"
2123
crv1 "github.com/crunchydata/postgres-operator/pkg/apis/crunchydata.com/v1"
2224

2325
"k8s.io/apimachinery/pkg/api/resource"
2426
)
2527

28+
func TestGetPasswordType(t *testing.T) {
29+
t.Run("valid", func(t *testing.T) {
30+
tests := map[string]pgpassword.PasswordType{
31+
"": pgpassword.MD5,
32+
"md5": pgpassword.MD5,
33+
"scram": pgpassword.SCRAM,
34+
"scram-sha-256": pgpassword.SCRAM,
35+
}
36+
37+
for passwordTypeStr, expected := range tests {
38+
t.Run(passwordTypeStr, func(t *testing.T) {
39+
passwordType, err := GetPasswordType(passwordTypeStr)
40+
if err != nil {
41+
t.Error(err)
42+
return
43+
}
44+
45+
if passwordType != expected {
46+
t.Errorf("password type %q should yield %d", passwordTypeStr, expected)
47+
}
48+
})
49+
}
50+
})
51+
52+
t.Run("invalid", func(t *testing.T) {
53+
tests := map[string]error{
54+
"magic": ErrPasswordTypeInvalid,
55+
"scram-sha-512": ErrPasswordTypeInvalid,
56+
}
57+
58+
for passwordTypeStr, expected := range tests {
59+
t.Run(passwordTypeStr, func(t *testing.T) {
60+
if _, err := GetPasswordType(passwordTypeStr); !errors.Is(err, expected) {
61+
t.Errorf("password type %q should yield error %q", passwordTypeStr, expected.Error())
62+
}
63+
})
64+
}
65+
})
66+
}
67+
2668
func TestValidateBackrestStorageTypeForCommand(t *testing.T) {
2769
cluster := &crv1.Pgcluster{
2870
Spec: crv1.PgclusterSpec{},

internal/apiserver/userservice/userimpl.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func CreateUser(request *msgs.CreateUserRequest, pgouser string) msgs.CreateUser
155155
}
156156

157157
// determine if the user passed in a valid password type
158-
passwordType, err := msgs.GetPasswordType(request.PasswordType)
158+
passwordType, err := apiserver.GetPasswordType(request.PasswordType)
159159
if err != nil {
160160
response.Status.Code = msgs.Error
161161
response.Status.Msg = err.Error()
@@ -601,7 +601,7 @@ func UpdateUser(request *msgs.UpdateUserRequest, pgouser string) msgs.UpdateUser
601601
}
602602

603603
// determine if the user passed in a valid password type
604-
if _, err := msgs.GetPasswordType(request.PasswordType); err != nil {
604+
if _, err := apiserver.GetPasswordType(request.PasswordType); err != nil {
605605
response.Status.Code = msgs.Error
606606
response.Status.Msg = err.Error()
607607
return response
@@ -940,7 +940,7 @@ func rotateExpiredPasswords(request *msgs.UpdateUserRequest, cluster *crv1.Pgclu
940940

941941
// get the password type. the error is already evaluated in a called
942942
// function
943-
passwordType, _ := msgs.GetPasswordType(request.PasswordType)
943+
passwordType, _ := apiserver.GetPasswordType(request.PasswordType)
944944

945945
// generate a new password. Check to see if the user passed in a particular
946946
// length of the password, or passed in a password to rotate (though that
@@ -1072,7 +1072,7 @@ func updateUser(request *msgs.UpdateUserRequest, cluster *crv1.Pgcluster) msgs.U
10721072
// Speaking of passwords...let's first determine if the user updated their
10731073
// password. See generatePassword for how precedence is given for password
10741074
// updates
1075-
passwordType, _ := msgs.GetPasswordType(request.PasswordType)
1075+
passwordType, _ := apiserver.GetPasswordType(request.PasswordType)
10761076
isChanged, password, hashedPassword, err := generatePassword(result.Username,
10771077
request.Password, passwordType, request.RotatePassword, request.PasswordLength)
10781078
// in the off-chance there is an error generating the password, record it

pkg/apiservermsgs/configmsgs.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@ See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/
1717

18-
import (
19-
"github.com/crunchydata/postgres-operator/internal/config"
20-
)
21-
2218
// ShowConfigResponse ...
2319
// swagger:model
2420
type ShowConfigResponse struct {
25-
Result config.PgoConfig
21+
Result interface{}
2622
Status
2723
}

pkg/apiservermsgs/usermsgs.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/
1717

18-
import (
19-
"errors"
20-
21-
pgpassword "github.com/crunchydata/postgres-operator/internal/postgres/password"
22-
)
23-
2418
type UpdateClusterLoginState int
2519

2620
// set the different values around whether or not to disable/enable a user's
@@ -31,19 +25,6 @@ const (
3125
UpdateUserLoginDisable
3226
)
3327

34-
// ErrPasswordTypeInvalid is used when a string that's not included in
35-
// PasswordTypeStrings is used
36-
var ErrPasswordTypeInvalid = errors.New("invalid password type. choices are (md5, scram-sha-256)")
37-
38-
// passwordTypeStrings is a mapping of strings of password types to their
39-
// corresponding value of the structured password type
40-
var passwordTypeStrings = map[string]pgpassword.PasswordType{
41-
"": pgpassword.MD5,
42-
"md5": pgpassword.MD5,
43-
"scram": pgpassword.SCRAM,
44-
"scram-sha-256": pgpassword.SCRAM,
45-
}
46-
4728
// CreateUserRequest contains the parameters that are passed in when an Operator
4829
// user requests to create a new PostgreSQL user
4930
// swagger:model
@@ -153,15 +134,3 @@ type UserResponseDetail struct {
153134
Username string
154135
ValidUntil string
155136
}
156-
157-
// GetPasswordType returns the enumerated password type based on the string, and
158-
// an error if it cannot match one
159-
func GetPasswordType(passwordTypeStr string) (pgpassword.PasswordType, error) {
160-
passwordType, ok := passwordTypeStrings[passwordTypeStr]
161-
162-
if !ok {
163-
return passwordType, ErrPasswordTypeInvalid
164-
}
165-
166-
return passwordType, nil
167-
}

pkg/apiservermsgs/usermsgs_test.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

pkg/events/eventing.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,12 @@ import (
2323
"reflect"
2424
"time"
2525

26-
crunchylog "github.com/crunchydata/postgres-operator/internal/logging"
2726
"github.com/nsqio/go-nsq"
2827
log "github.com/sirupsen/logrus"
2928
)
3029

3130
// String returns the string form for a given LogLevel
3231
func Publish(e EventInterface) error {
33-
// Add logging configuration
34-
crunchylog.CrunchyLogger(crunchylog.SetParameters())
3532
eventAddr := os.Getenv("EVENT_ADDR")
3633
if eventAddr == "" {
3734
return errors.New("EVENT_ADDR not set")

0 commit comments

Comments
 (0)