diff --git a/.cases/termsandconditions.sh b/.cases/termsandconditions.sh new file mode 100644 index 000000000..7f6fddbbe --- /dev/null +++ b/.cases/termsandconditions.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +## 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 \ No newline at end of file diff --git a/Makefile b/Makefile index 8c13521f0..70c11350b 100644 --- a/Makefile +++ b/Makefile @@ -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" @@ -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) diff --git a/internal/build/build.go b/internal/build/build.go index 094ff180f..b87a93312 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -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" // DefaultPageSize is the default number of items per page when using list commands DefaultPageSize = "10" diff --git a/pkg/ams/TermsAndConditionsSpec.go b/pkg/ams/TermsAndConditionsSpec.go new file mode 100644 index 000000000..02408c92d --- /dev/null +++ b/pkg/ams/TermsAndConditionsSpec.go @@ -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"` +} diff --git a/pkg/ams/ams.go b/pkg/ams/ams.go index 59cf3f850..b293e829c 100644 --- a/pkg/ams/ams.go +++ b/pkg/ams/ams.go @@ -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 { diff --git a/pkg/ams/terms.go b/pkg/ams/terms.go new file mode 100644 index 000000000..fd51f1a7b --- /dev/null +++ b/pkg/ams/terms.go @@ -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) + return fallbackTocSpec + } + response, err := client.Do(req) + if err != nil || response == nil { + logger.Debug("Fetching remote terms failed with error ", err) + return fallbackTocSpec + } + defer response.Body.Close() + + specJson, err := ioutil.ReadAll(response.Body) + if err != nil { + 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 +} diff --git a/pkg/cmd/kafka/create/create.go b/pkg/cmd/kafka/create/create.go index 398bafcfa..babd686c6 100644 --- a/pkg/cmd/kafka/create/create.go +++ b/pkg/cmd/kafka/create/create.go @@ -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 } diff --git a/pkg/cmd/registry/create/create.go b/pkg/cmd/registry/create/create.go index 97856b8d7..5197c2320 100644 --- a/pkg/cmd/registry/create/create.go +++ b/pkg/cmd/registry/create/create.go @@ -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" @@ -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 }