Skip to content

Commit

Permalink
Merge branch 'master' into configure-bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellen-Yi-Dong authored Jan 25, 2023
2 parents dcae4f4 + c3375de commit 10e5efc
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 9 deletions.
3 changes: 3 additions & 0 deletions config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type EntitlementsConfigKeysType struct {
SubAPIBasePath string
CompAPIBasePath string
RunBundleSync string
EntitleAll string
}

// Keys is a struct that houses all the env variables key names
Expand All @@ -64,6 +65,7 @@ var Keys = EntitlementsConfigKeysType{
SubAPIBasePath: "SUB_API_BASE_PATH",
CompAPIBasePath: "COMP_API_BASE_PATH",
RunBundleSync: "RUN_BUNDLE_SYNC",
EntitleAll: "ENTITLE_ALL",
}

func getBaseFeaturesPath(options *viper.Viper) string {
Expand Down Expand Up @@ -140,6 +142,7 @@ func initialize() {
options.SetDefault(Keys.SubAPIBasePath, "/svcrest/subscription/v5/")
options.SetDefault(Keys.CompAPIBasePath, "/v1/screening")
options.SetDefault(Keys.RunBundleSync, false)
options.SetDefault(Keys.EntitleAll, false)

options.SetEnvPrefix("ENT")
options.AutomaticEnv()
Expand Down
58 changes: 51 additions & 7 deletions controllers/controllers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package controllers
import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/http/httptest"
"errors"
"os"
"testing"

. "github.com/RedHatInsights/entitlements-api-go/types"
"github.com/redhatinsights/platform-go-middlewares/identity"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/redhatinsights/platform-go-middlewares/identity"
)

const DEFAULT_ORG_ID string = "4384938490324"
Expand All @@ -30,7 +31,7 @@ func testRequest(method string, path string, accnum string, orgid string, isinte
AccountNumber: accnum,
User: identity.User{
Internal: isinternal,
Email: email,
Email: email,
},
Internal: identity.Internal{
OrgID: orgid,
Expand Down Expand Up @@ -265,13 +266,13 @@ var _ = Describe("Identity Controller", func() {
It("should set values based on response from the featureStatus request", func() {
fakeResponse := SubscriptionsResponse{
StatusCode: 200,
Data: FeatureStatus{
Data: FeatureStatus{
[]Feature{
{ Name: "TestBundle1", IsEval: false, Entitled: false },
{ Name: "TestBundle2", IsEval: true, Entitled: true },
{Name: "TestBundle1", IsEval: false, Entitled: false},
{Name: "TestBundle2", IsEval: true, Entitled: true},
},
},
CacheHit: false,
CacheHit: false,
}

rr, body, _ := testRequestWithDefaultOrgId("GET", "/", fakeGetFeatureStatus(DEFAULT_ORG_ID, fakeResponse))
Expand All @@ -284,6 +285,49 @@ var _ = Describe("Identity Controller", func() {
Expect(body["TestBundle6"].IsEntitled).To(Equal(false))
})
})

Context("When ENT_ENTITLE_ALL is set", func() {
AfterEach(func() {
os.Setenv("ENT_ENTITLE_ALL", "")
})
fakeResponse := SubscriptionsResponse{
StatusCode: 200,
Data: FeatureStatus{},
CacheHit: false,
}

It("should skip IT calls and entitle all bundles when true", func() {
os.Setenv("ENT_ENTITLE_ALL", "true")
rr, body, _ := testRequest("GET", "/", "-1", DEFAULT_ORG_ID, true, DEFAULT_EMAIL, fakeGetFeatureStatus(DEFAULT_ORG_ID, fakeResponse))
expectPass(rr.Result())
Expect(body["TestBundle1"].IsEntitled).To(Equal(true))
Expect(body["TestBundle2"].IsEntitled).To(Equal(true))
Expect(body["TestBundle3"].IsEntitled).To(Equal(true))
Expect(body["TestBundle4"].IsEntitled).To(Equal(true))
Expect(body["TestBundle5"].IsEntitled).To(Equal(true))
})

It("should return as normal when false", func() {
os.Setenv("ENT_ENTITLE_ALL", "false")
rr, body, _ := testRequest("GET", "/", "-1", DEFAULT_ORG_ID, true, DEFAULT_EMAIL, fakeGetFeatureStatus(DEFAULT_ORG_ID, fakeResponse))
expectPass(rr.Result())
Expect(body["TestBundle1"].IsEntitled).To(Equal(false))
Expect(body["TestBundle2"].IsEntitled).To(Equal(false))
Expect(body["TestBundle3"].IsEntitled).To(Equal(true))
Expect(body["TestBundle4"].IsEntitled).To(Equal(false))
Expect(body["TestBundle5"].IsEntitled).To(Equal(false))
})

It("should return as normal when unset", func() {
rr, body, _ := testRequest("GET", "/", "-1", DEFAULT_ORG_ID, true, DEFAULT_EMAIL, fakeGetFeatureStatus(DEFAULT_ORG_ID, fakeResponse))
expectPass(rr.Result())
Expect(body["TestBundle1"].IsEntitled).To(Equal(false))
Expect(body["TestBundle2"].IsEntitled).To(Equal(false))
Expect(body["TestBundle3"].IsEntitled).To(Equal(true))
Expect(body["TestBundle4"].IsEntitled).To(Equal(false))
Expect(body["TestBundle5"].IsEntitled).To(Equal(false))
})
})
})

func BenchmarkRequest(b *testing.B) {
Expand Down
23 changes: 21 additions & 2 deletions controllers/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func SetBundleInfo(yamlFilePath string) error {
// GetFeatureStatus calls the IT subs service features endpoint and returns the entitlements for specified features/bundles
var GetFeatureStatus = func(orgID string) types.SubscriptionsResponse {
item := cache.Get(orgID)
entitleAll := config.GetConfig().Options.GetString(config.Keys.EntitleAll)

if item != nil && !item.Expired() {
return types.SubscriptionsResponse{
Expand All @@ -69,6 +70,14 @@ var GetFeatureStatus = func(orgID string) types.SubscriptionsResponse {
}
}

if entitleAll == "true" {
return types.SubscriptionsResponse{
StatusCode: 200,
Data: types.FeatureStatus{},
CacheHit: false,
}
}

q := config.GetConfig().Options.GetString(config.Keys.FeaturesPath)
req := config.GetConfig().Options.GetString(config.Keys.SubsHost) +
"/svcrest/subscription/v5/featureStatus" +
Expand Down Expand Up @@ -102,7 +111,7 @@ var GetFeatureStatus = func(orgID string) types.SubscriptionsResponse {
var FeatureStatus types.FeatureStatus
json.Unmarshal(body, &FeatureStatus)

cache.Set(orgID, FeatureStatus, time.Minute*10)
cache.Set(orgID, FeatureStatus, time.Minute*30)

return types.SubscriptionsResponse{
StatusCode: resp.StatusCode,
Expand All @@ -127,6 +136,10 @@ func failOnDependencyError(errMsg string, res types.SubscriptionsResponse, w htt
http.Error(w, string(errorResponsejson), 500)
}

func setBundlePayload(entitle bool, trial bool) types.EntitlementsSection {
return types.EntitlementsSection{IsEntitled: entitle, IsTrial: trial}
}

// Index the handler for GETs to /api/entitlements/v1/services/
func Index() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
Expand Down Expand Up @@ -171,6 +184,12 @@ func Index() func(http.ResponseWriter, *http.Request) {
for _, b := range bundleInfo {
entitle := true
trial := false
entitleAll := config.GetConfig().Options.GetString(config.Keys.EntitleAll)

if entitleAll == "true" {
entitlementsResponse[b.Name] = setBundlePayload(entitle, trial)
continue
}

if len(b.Skus) > 0 {
entitle = false
Expand All @@ -193,7 +212,7 @@ func Index() func(http.ResponseWriter, *http.Request) {
if b.UseIsInternal {
entitle = validAccNum && isInternal && validEmailMatch
}
entitlementsResponse[b.Name] = types.EntitlementsSection{IsEntitled: entitle, IsTrial: trial}
entitlementsResponse[b.Name] = setBundlePayload(entitle, trial)
}

obj, err := json.Marshal(entitlementsResponse)
Expand Down
6 changes: 6 additions & 0 deletions deployment/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ objects:
name: default-entitlements-config
containers:
- env:
- name: ENT_ENTITLE_ALL
value: ${ENTITLE_ALL}
- name: ENT_CERTS_FROM_ENV
value: 'true'
- name: ENT_CA_PATH
Expand Down Expand Up @@ -195,3 +197,7 @@ parameters:
- description: Application allow list for new bundles with skus to update the feature in the IT features API
name: BUNDLES_SKUS_ALLOW_LIST
value: ansible,smart_management_rods,rhoam,rhosak,openshift,rhel
- description: Flag to determine whether or not to entitle all by default and mock calls to IT
name: ENTITLE_ALL
required: false
value: 'false'

0 comments on commit 10e5efc

Please sign in to comment.