Skip to content

Commit 2f6268a

Browse files
jkatzJonathan S. Katz
authored andcommitted
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 7b197d4 commit 2f6268a

File tree

8 files changed

+78
-113
lines changed

8 files changed

+78
-113
lines changed

internal/apiserver/common.go

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

2424
"github.com/crunchydata/postgres-operator/internal/config"
25+
pgpassword "github.com/crunchydata/postgres-operator/internal/postgres/password"
2526
crv1 "github.com/crunchydata/postgres-operator/pkg/apis/crunchydata.com/v1"
2627
log "github.com/sirupsen/logrus"
2728
kerrors "k8s.io/apimachinery/pkg/api/errors"
@@ -48,6 +49,9 @@ var (
4849
ErrDBContainerNotFound = errors.New("\"database\" container not found in pod")
4950
// ErrLabelInvalid indicates that a label is invalid
5051
ErrLabelInvalid = errors.New("invalid label")
52+
// ErrPasswordTypeInvalid is used when a string that's not included in
53+
// PasswordTypeStrings is used
54+
ErrPasswordTypeInvalid = errors.New("invalid password type. choices are (md5, scram-sha-256)")
5155
// ErrStandbyNotAllowed contains the error message returned when an API call is not
5256
// permitted because it involves a cluster that is in standby mode
5357
ErrStandbyNotAllowed = errors.New("Action not permitted because standby mode is enabled")
@@ -58,6 +62,15 @@ var (
5862
"Operator installation")
5963
)
6064

65+
// passwordTypeStrings is a mapping of strings of password types to their
66+
// corresponding value of the structured password type
67+
var passwordTypeStrings = map[string]pgpassword.PasswordType{
68+
"": pgpassword.MD5,
69+
"md5": pgpassword.MD5,
70+
"scram": pgpassword.SCRAM,
71+
"scram-sha-256": pgpassword.SCRAM,
72+
}
73+
6174
func CreateRMDataTask(clusterName, replicaName, taskName string, deleteBackups, deleteData, isReplica, isBackup bool, ns, clusterPGHAScope string) error {
6275
var err error
6376

@@ -100,6 +113,18 @@ func GetBackrestStorageTypes() []string {
100113
return backrestStorageTypes
101114
}
102115

116+
// GetPasswordType returns the enumerated password type based on the string, and
117+
// an error if it cannot match one
118+
func GetPasswordType(passwordTypeStr string) (pgpassword.PasswordType, error) {
119+
passwordType, ok := passwordTypeStrings[passwordTypeStr]
120+
121+
if !ok {
122+
return passwordType, ErrPasswordTypeInvalid
123+
}
124+
125+
return passwordType, nil
126+
}
127+
103128
// IsValidPVC determines if a PVC with the name provided exits
104129
func IsValidPVC(pvcName, ns string) bool {
105130
pvc, err := Clientset.CoreV1().PersistentVolumeClaims(ns).Get(pvcName, metav1.GetOptions{})

internal/apiserver/common_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,51 @@ import (
2121
"strings"
2222
"testing"
2323

24+
pgpassword "github.com/crunchydata/postgres-operator/internal/postgres/password"
25+
2426
"k8s.io/apimachinery/pkg/api/resource"
2527
)
2628

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

internal/apiserver/userservice/userimpl.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ func CreateUser(request *msgs.CreateUserRequest, pgouser string) msgs.CreateUser
168168
}
169169

170170
// determine if the user passed in a valid password type
171-
passwordType, err := msgs.GetPasswordType(request.PasswordType)
172-
171+
passwordType, err := apiserver.GetPasswordType(request.PasswordType)
173172
if err != nil {
174173
response.Status.Code = msgs.Error
175174
response.Status.Msg = err.Error()
@@ -620,7 +619,7 @@ func UpdateUser(request *msgs.UpdateUserRequest, pgouser string) msgs.UpdateUser
620619
}
621620

622621
// determine if the user passed in a valid password type
623-
if _, err := msgs.GetPasswordType(request.PasswordType); err != nil {
622+
if _, err := apiserver.GetPasswordType(request.PasswordType); err != nil {
624623
response.Status.Code = msgs.Error
625624
response.Status.Msg = err.Error()
626625
return response
@@ -965,7 +964,7 @@ func rotateExpiredPasswords(request *msgs.UpdateUserRequest, cluster *crv1.Pgclu
965964

966965
// get the password type. the error is already evaluated in a called
967966
// function
968-
passwordType, _ := msgs.GetPasswordType(request.PasswordType)
967+
passwordType, _ := apiserver.GetPasswordType(request.PasswordType)
969968

970969
// generate a new password. Check to see if the user passed in a particular
971970
// length of the password, or passed in a password to rotate (though that
@@ -1097,7 +1096,7 @@ func updateUser(request *msgs.UpdateUserRequest, cluster *crv1.Pgcluster) msgs.U
10971096
// Speaking of passwords...let's first determine if the user updated their
10981097
// password. See generatePassword for how precedence is given for password
10991098
// updates
1100-
passwordType, _ := msgs.GetPasswordType(request.PasswordType)
1099+
passwordType, _ := apiserver.GetPasswordType(request.PasswordType)
11011100
isChanged, password, hashedPassword, err := generatePassword(result.Username,
11021101
request.Password, passwordType, request.RotatePassword, request.PasswordLength)
11031102

pgo/cmd/user.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"os"
2121
"strings"
2222

23+
"github.com/crunchydata/postgres-operator/internal/apiserver"
2324
utiloperator "github.com/crunchydata/postgres-operator/internal/util"
2425
"github.com/crunchydata/postgres-operator/pgo/api"
2526
"github.com/crunchydata/postgres-operator/pgo/util"
@@ -88,7 +89,7 @@ func createUser(args []string, ns string) {
8889
}
8990

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

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

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 & 33 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,21 +25,6 @@ const (
3125
UpdateUserLoginDisable
3226
)
3327

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

pkg/apiservermsgs/usermsgs_test.go

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

pkg/events/eventing.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,16 @@ import (
1919
"encoding/json"
2020
"errors"
2121
"fmt"
22-
crunchylog "github.com/crunchydata/postgres-operator/internal/logging"
23-
"github.com/nsqio/go-nsq"
24-
log "github.com/sirupsen/logrus"
2522
"os"
2623
"reflect"
2724
"time"
25+
26+
"github.com/nsqio/go-nsq"
27+
log "github.com/sirupsen/logrus"
2828
)
2929

3030
// String returns the string form for a given LogLevel
3131
func Publish(e EventInterface) error {
32-
//Add logging configuration
33-
crunchylog.CrunchyLogger(crunchylog.SetParameters())
3432
eventAddr := os.Getenv("EVENT_ADDR")
3533
if eventAddr == "" {
3634
return errors.New("EVENT_ADDR not set")

0 commit comments

Comments
 (0)