Skip to content

Commit

Permalink
Refactored common code
Browse files Browse the repository at this point in the history
  • Loading branch information
verbanicm committed Aug 10, 2021
1 parent f861fe7 commit 229c264
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 26 deletions.
15 changes: 3 additions & 12 deletions pkg/skaffold/instrumentation/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"math/rand"
"os"
"path/filepath"
"regexp"
"strconv"
"time"

Expand All @@ -49,6 +48,7 @@ import (

"github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/cmd/statik"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/user"
"github.com/GoogleContainerTools/skaffold/proto/v1"
)

Expand Down Expand Up @@ -181,17 +181,8 @@ func createMetrics(ctx context.Context, meter skaffoldMeter) {
randLabel,
}

// check each allowed user key for pattern match
for allowedUser := range constants.AllowedUsers {
matched, err := regexp.MatchString(fmt.Sprintf(constants.AllowedUserPattern, allowedUser), meter.User)
if err != nil {
panic(fmt.Sprintf("error matching allowed user: %v", err))
}

if matched || allowedUser == meter.User {
sharedLabels = append(sharedLabels, attribute.String("user", meter.User))
break
}
if allowedUser := user.IsAllowedUser(meter.User); allowedUser {
sharedLabels = append(sharedLabels, attribute.String("user", meter.User))
}
labels = append(labels, sharedLabels...)

Expand Down
39 changes: 39 additions & 0 deletions pkg/skaffold/user/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2021 The Skaffold 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 user

import (
"fmt"
"regexp"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
)

func IsAllowedUser(user string) bool {
for allowedUser := range constants.AllowedUsers {
matched, err := regexp.MatchString(fmt.Sprintf(constants.AllowedUserPattern, allowedUser), user)
if err != nil {
panic(fmt.Sprintf("error matching allowed user: %v", err))
}

if matched {
return true
}
}

return false
}
68 changes: 68 additions & 0 deletions pkg/skaffold/user/user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2021 The Skaffold 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 user

import (
"testing"

"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestAllowedUser(t *testing.T) {
tests := []struct {
description string
user string
expected bool
}{
{
description: "user in allowed list",
user: "vsc",
expected: true,
},
{
description: "user in allowed list",
user: "cloud-deploy",
expected: true,
},
{
description: "user in allowed list with valid pattern",
user: "cloud-deploy/dev",
expected: true,
},
{
description: "user in allowed list with invalid pattern (only slash)",
user: "cloud-deploy|dev",
expected: false,
},
{
description: "user in allowed list with invalid pattern (suffix required)",
user: "cloud-deploy/",
expected: false,
},
{
description: "user not in allowed list",
user: "test-user",
expected: false,
},
}
for _, test := range tests {
testutil.Run(t, "", func(t *testutil.T) {
allowedUser := IsAllowedUser(test.user)
t.CheckDeepEqual(test.expected, allowedUser)
})
}
}
18 changes: 4 additions & 14 deletions pkg/skaffold/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ package version

import (
"fmt"
"regexp"
"runtime"
"strings"

"github.com/blang/semver"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/user"
)

var version, gitCommit, buildDate, client string
Expand Down Expand Up @@ -58,18 +57,9 @@ var Get = func() *Info {
}
}

var SetClient = func(user string) {
// check each allowed user key for pattern match
for allowedUser := range constants.AllowedUsers {
matched, err := regexp.MatchString(fmt.Sprintf(constants.AllowedUserPattern, allowedUser), user)
if err != nil {
panic(fmt.Sprintf("error matching allowed user: %v", err))
}

if matched || allowedUser == user {
client = user
break
}
var SetClient = func(userAgent string) {
if allowedUser := user.IsAllowedUser(userAgent); allowedUser {
client = userAgent
}
}

Expand Down

0 comments on commit 229c264

Please sign in to comment.