Skip to content

feat: only display license warnings to privileged users #10096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,10 +835,18 @@ func (r *RootCmd) checkWarnings(i *clibase.Invocation, client *codersdk.Client)
ctx, cancel := context.WithTimeout(i.Context(), 10*time.Second)
defer cancel()

user, err := client.User(ctx, codersdk.Me)
if err != nil {
return xerrors.Errorf("get user me: %w", err)
}

entitlements, err := client.Entitlements(ctx)
if err == nil {
for _, w := range entitlements.Warnings {
_, _ = fmt.Fprintln(i.Stderr, pretty.Sprint(cliui.DefaultStyles.Warn, w))
// Don't show warning to regular users.
if len(user.Roles) > 0 {
for _, w := range entitlements.Warnings {
_, _ = fmt.Fprintln(i.Stderr, pretty.Sprint(cliui.DefaultStyles.Warn, w))
}
Comment on lines +845 to +849
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really ideal. Checking the roles length would fail if we added back in org-member roles for example.

A better check is to check the capability of a user. Such as checking if the user can read deploymentValues or something.

}
}
return nil
Expand Down
1 change: 1 addition & 0 deletions enterprise/cli/licenses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ func newFakeLicenseAPI(t *testing.T) http.Handler {
r.Post("/api/v2/licenses", a.postLicense)
r.Get("/api/v2/licenses", a.licenses)
r.Get("/api/v2/buildinfo", a.noop)
r.Get("/api/v2/users/me", a.noop)
r.Delete("/api/v2/licenses/{id}", a.deleteLicense)
r.Get("/api/v2/entitlements", a.entitlements)
return r
Expand Down
50 changes: 50 additions & 0 deletions enterprise/cli/root_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package cli_test

import (
"bytes"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/cli/config"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/enterprise/cli"
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
)

func newCLI(t *testing.T, args ...string) (*clibase.Invocation, config.Root) {
Expand All @@ -27,3 +31,49 @@ func TestEnterpriseHandlersOK(t *testing.T) {

clitest.HandlersOK(t, cmd)
}

func TestCheckWarnings(t *testing.T) {
t.Parallel()

t.Run("LicenseWarningForPrivilegedRoles", func(t *testing.T) {
t.Parallel()
client, _ := coderdenttest.New(t, &coderdenttest.Options{
LicenseOptions: &coderdenttest.LicenseOptions{
ExpiresAt: time.Now().Add(time.Hour * 24),
},
})

inv, conf := newCLI(t, "list")

var buf bytes.Buffer
inv.Stderr = &buf
clitest.SetupConfig(t, client, conf)

err := inv.Run()
require.NoError(t, err)

require.Contains(t, buf.String(), "Your license expires in 1 day.")
})

t.Run("NoLicenseWarningForRegularUser", func(t *testing.T) {
t.Parallel()
adminClient, admin := coderdenttest.New(t, &coderdenttest.Options{
LicenseOptions: &coderdenttest.LicenseOptions{
ExpiresAt: time.Now().Add(time.Hour * 24),
},
})

client, _ := coderdtest.CreateAnotherUser(t, adminClient, admin.OrganizationID)

inv, conf := newCLI(t, "list")

var buf bytes.Buffer
inv.Stderr = &buf
clitest.SetupConfig(t, client, conf)

err := inv.Run()
require.NoError(t, err)

require.NotContains(t, buf.String(), "Your license expires")
})
}