Skip to content
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

Use remote spec for terms and conditions #1276

Merged
merged 6 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
20 changes: 20 additions & 0 deletions .cases/termsandconditions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
Copy link
Contributor

Choose a reason for hiding this comment

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

Getting permission denied on this file. Should it be automatically executable? On my Linux machine it was, could be different on MacOS though..

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Amazing feedback. I will move those to md files as sh doesnt make sense when we copy commands to execute
We can have template for new verification


## Terms and Conditions acceptance

## Requirements
## Create new account without any terms and conditions accepted.

## Framework

alias rhoas=$(go env GOPATH)/bin/rhoas

## Cases

rhoas service-registry create --name=test -v
## In order to be able to create a new instance, you must first review and accept the terms and conditions:
## https://www.redhat.com/wapps/tnc/ackrequired?site=ocm&event=register

rhoas kafka create --name=test --provider=aws --region=eu-east1 -v
## In order to be able to create a new instance, you must first review and accept the terms and conditions:
## https://www.redhat.com/wapps/tnc/ackrequired?site=ocm&event=onlineService
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ SHELL = bash
RHOAS_VERSION ?= "dev"
REPOSITORY_OWNER ?= "redhat-developer"
REPOSITORY_NAME ?= "app-services-cli"
TERMS_REVIEW_EVENT_CODE ?= "register"
TERMS_REVIEW_SITE_CODE ?= "ocm"
TERMS_SPEC_URL ?= "https://raw.githubusercontent.com/redhat-developer/app-services-ui/main/static/configs/terms-conditions-spec.json"
SSO_REDIRECT_PATH ?= "sso-redhat-callback"
MAS_SSO_REDIRECT_PATH ?= "mas-sso-callback"

Expand All @@ -17,8 +16,7 @@ DEFAULT_PAGE_SIZE ?= "10"
GO_LDFLAGS := -X github.com/redhat-developer/app-services-cli/internal/build.Version=$(RHOAS_VERSION) $(GO_LDFLAGS)
GO_LDFLAGS := -X github.com/redhat-developer/app-services-cli/internal/build.RepositoryOwner=$(REPOSITORY_OWNER) $(GO_LDFLAGS)
GO_LDFLAGS := -X github.com/redhat-developer/app-services-cli/internal/build.RepositoryName=$(REPOSITORY_NAME) $(GO_LDFLAGS)
GO_LDFLAGS := -X github.com/redhat-developer/app-services-cli/internal/build.TermsReviewEventCode=$(TERMS_REVIEW_EVENT_CODE) $(GO_LDFLAGS)
GO_LDFLAGS := -X github.com/redhat-developer/app-services-cli/internal/build.TermsReviewSiteCode=$(TERMS_REVIEW_SITE_CODE) $(GO_LDFLAGS)
GO_LDFLAGS := -X github.com/redhat-developer/app-services-cli/internal/build.TermsReviewSpecURL=$(TERMS_SPEC_URL) $(GO_LDFLAGS)
GO_LDFLAGS := -X github.com/redhat-developer/app-services-cli/internal/build.DefaultPageSize=$(DEFAULT_PAGE_SIZE) $(GO_LDFLAGS)
GO_LDFLAGS := -X github.com/redhat-developer/app-services-cli/internal/build.DefaultPageNumber=$(DEFAULT_PAGE_NUMBER) $(GO_LDFLAGS)
GO_LDFLAGS := -X github.com/redhat-developer/app-services-cli/internal/build.SSORedirectPath=$(SSO_REDIRECT_PATH) $(GO_LDFLAGS)
Expand Down
7 changes: 2 additions & 5 deletions internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ var (
// RepositoryName is the remote GitHub repository for the releases
RepositoryName = "app-services-cli"

// TermsReviewEventCode is the event code used when checking the terms review
TermsReviewEventCode = "register"

// TermsReviewSiteCode is the site code used when checking the terms review
TermsReviewSiteCode = "ocm"
// TermsReviewSpecURL Url used to download terms and conditions specification
TermsReviewSpecURL = "https://raw.githubusercontent.com/redhat-developer/app-services-ui/main/static/configs/terms-conditions-spec.json"
craicoverflow marked this conversation as resolved.
Show resolved Hide resolved

// DefaultPageSize is the default number of items per page when using list commands
DefaultPageSize = "10"
Expand Down
11 changes: 11 additions & 0 deletions pkg/ams/TermsAndConditionsSpec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ams

type TermsAndConditionsSpec struct {
Kafka ServiceTermsSpec `json:"kafka"`
ServiceRegistry ServiceTermsSpec `json:"service-registry"`
}

type ServiceTermsSpec struct {
EventCode string `json:"EventCode"`
SiteCode string `json:"SiteCode"`
}
7 changes: 3 additions & 4 deletions pkg/ams/ams.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"context"
"errors"

"github.com/redhat-developer/app-services-cli/internal/build"
"github.com/redhat-developer/app-services-cli/pkg/api/ams/amsclient"
"github.com/redhat-developer/app-services-cli/pkg/connection"
)

func CheckTermsAccepted(ctx context.Context, conn connection.Connection) (accepted bool, redirectURI string, err error) {
func CheckTermsAccepted(ctx context.Context, spec ServiceTermsSpec, conn connection.Connection) (accepted bool, redirectURI string, err error) {
termsReview, _, err := conn.API().AccountMgmt().
ApiAuthorizationsV1SelfTermsReviewPost(ctx).
SelfTermsReview(amsclient.SelfTermsReview{
EventCode: &build.TermsReviewEventCode,
SiteCode: &build.TermsReviewSiteCode,
EventCode: &spec.EventCode,
SiteCode: &spec.SiteCode,
}).
Execute()
if err != nil {
Expand Down
59 changes: 59 additions & 0 deletions pkg/ams/terms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package ams

import (
"context"
"encoding/json"
"io/ioutil"
"net/http"

"github.com/redhat-developer/app-services-cli/internal/build"
"github.com/redhat-developer/app-services-cli/pkg/logging"
)

// Contains specification for terms and condition parameters
// NOTE: Before updating this fallback file
// Please update source at https://github.com/redhat-developer/app-services-ui/blob/main/static/configs/terms-conditions-spec.json
var fallbackTocSpec = TermsAndConditionsSpec{
Kafka: ServiceTermsSpec{
EventCode: "register",
SiteCode: "ocm",
},
ServiceRegistry: ServiceTermsSpec{
EventCode: "onlineService",
SiteCode: "ocm",
},
}

// GetRemoteTermsSpec fetch event and site code information associated with the services
// Function is used to dynamically download new terms and conditions specifications
// without forcing end users to update their CLI.
func GetRemoteTermsSpec(context *context.Context, logger logging.Logger) TermsAndConditionsSpec {
client := &http.Client{}
req, err := http.NewRequestWithContext(*context, http.MethodGet, build.TermsReviewSpecURL, nil)
if err != nil {
logger.Debug("Fetching remote terms failed with error ", err)
wtrocki marked this conversation as resolved.
Show resolved Hide resolved
return fallbackTocSpec
}
response, err := client.Do(req)
if err != nil || response.Body == nil {
wtrocki marked this conversation as resolved.
Show resolved Hide resolved
logger.Debug("Fetching remote terms failed with error ", err)
return fallbackTocSpec
}
defer response.Body.Close()

specJson, err := ioutil.ReadAll(response.Body)
if err != nil {
craicoverflow marked this conversation as resolved.
Show resolved Hide resolved
logger.Debug("Reading remote terms failed with error ", err)
return fallbackTocSpec
}

logger.Debug("Terms spec: ", specJson)

var termsAndConditionsSpec TermsAndConditionsSpec
err = json.Unmarshal([]byte(specJson), &termsAndConditionsSpec)
if err != nil {
logger.Debug("Parsing remote terms failed with error ", err)
return fallbackTocSpec
}
return termsAndConditionsSpec
}
3 changes: 2 additions & 1 deletion pkg/cmd/kafka/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ func runCreate(opts *options) error {

// the user must have accepted the terms and conditions from the provider
// before they can create a kafka instance
termsAccepted, termsURL, err := ams.CheckTermsAccepted(opts.Context, conn)
termsSpec := ams.GetRemoteTermsSpec(&opts.Context, opts.Logger)
termsAccepted, termsURL, err := ams.CheckTermsAccepted(opts.Context, termsSpec.Kafka, conn)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/registry/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"fmt"

"github.com/redhat-developer/app-services-cli/pkg/ams"
"github.com/redhat-developer/app-services-cli/pkg/icon"

"github.com/redhat-developer/app-services-cli/pkg/localize"
"github.com/redhat-developer/app-services-cli/pkg/serviceregistry"

"github.com/redhat-developer/app-services-cli/pkg/ams"
"github.com/redhat-developer/app-services-cli/pkg/cmd/flag"
flagutil "github.com/redhat-developer/app-services-cli/pkg/cmdutil/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/connection"
Expand Down Expand Up @@ -119,7 +119,8 @@ func runCreate(opts *options) error {

// the user must have accepted the terms and conditions from the provider
// before they can create a registry instance
termsAccepted, termsURL, err := ams.CheckTermsAccepted(opts.Context, conn)
termsSpec := ams.GetRemoteTermsSpec(&opts.Context, opts.Logger)
termsAccepted, termsURL, err := ams.CheckTermsAccepted(opts.Context, termsSpec.ServiceRegistry, conn)
if err != nil {
return err
}
Expand Down