Skip to content

Commit

Permalink
feat: add internal authlevel
Browse files Browse the repository at this point in the history
  • Loading branch information
hysyeah committed Sep 19, 2024
1 parent fb4d1c2 commit be51fb9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
1 change: 1 addition & 0 deletions internal/authorization/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
deny = "deny"
public = "public"
system = "system"
internal = "internal"
)

const (
Expand Down
70 changes: 46 additions & 24 deletions internal/authorization/ts_app_authorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,19 +463,19 @@ func (t *TsAuthorizer) getAppRules(position int, app *application.Application,
if len(app.Spec.Entrances) > 1 {
entranceId += strconv.Itoa(index)
}
domains := []string{
localDomains := []string{
fmt.Sprintf("%s.local.%s", entranceId, userInfo.Zone),
}
domains := []string{
fmt.Sprintf("%s.%s", entranceId, userInfo.Zone),
}

if customDomainExists {
entranceCustomDomain, ok := customDomain[entrance.Name]
if ok {
if entranceCustomDomain.ThirdLevelDomain != "" {
domains = append(domains, []string{
fmt.Sprintf("%s.local.%s", entranceCustomDomain.ThirdLevelDomain, userInfo.Zone),
fmt.Sprintf("%s.%s", entranceCustomDomain.ThirdLevelDomain, userInfo.Zone),
}...)
localDomains = append(localDomains, fmt.Sprintf("%s.local.%s", entranceCustomDomain.ThirdLevelDomain, userInfo.Zone))
domains = append(domains, fmt.Sprintf("%s.%s", entranceCustomDomain.ThirdLevelDomain, userInfo.Zone))
}

if entranceCustomDomain.ThirdPartyDomain != "" {
Expand All @@ -491,29 +491,35 @@ func (t *TsAuthorizer) getAppRules(position int, app *application.Application,
}
}

nonPolicy := func(p Level) {
nonPolicy := func(p Level, domains []string) {
rule := &AccessControlRule{
Position: position,
Policy: p,
}
ruleAddDomain(domains, rule)

rules = append(rules, rule)
position++
}

defaulPolicy := userAuth.appDefaultPolicy
if entrance.AuthLevel != "" && entrance.AuthLevel == "public" {
defaulPolicy = NewLevel(entrance.AuthLevel)
defaultPolicy := userAuth.appDefaultPolicy
defaultLocalPolicy := userAuth.appDefaultPolicy
if entrance.AuthLevel != "" && entrance.AuthLevel == public {
defaultPolicy = NewLevel(entrance.AuthLevel)
}
if entrance.AuthLevel != "" && entrance.AuthLevel == internal {
defaultLocalPolicy = NewLevel(public)
}

if !policyExists {
nonPolicy(defaulPolicy)
nonPolicy(defaultPolicy, domains)
nonPolicy(defaultLocalPolicy, localDomains)
continue
}

policy, ok := policies[entrance.Name]
if !ok {
nonPolicy(defaulPolicy)
nonPolicy(defaultPolicy, domains)
nonPolicy(defaultLocalPolicy, localDomains)
continue
}

Expand All @@ -526,6 +532,11 @@ func (t *TsAuthorizer) getAppRules(position int, app *application.Application,
}
}

appendRule := func(rule *AccessControlRule) {
rules = append(rules, rule)
position++
}

if policy.SubPolicies != nil {
for _, sp := range policy.SubPolicies {
// t.log.Debugf("add app %s rules %s on resource %s", app.Spec.Name, sp.Policy, sp.URI)
Expand All @@ -546,45 +557,56 @@ func (t *TsAuthorizer) getAppRules(position int, app *application.Application,
}
ruleAddResources(resources, rule)
ruleAddDomain(domains, rule)
ruleAddDomain(localDomains, rule)

rules = append(rules, rule)

position++
appendRule(rule)
} // end for policy.SubPolicies.
} // end if.

// add app others resource to default policy.
othersExp := regexp.MustCompile("^/.+")
othersResources := []regexp.Regexp{*othersExp}

if entrance.AuthLevel != "public" {
defaulPolicy = getLevel(policy.DefaultPolicy)
if entrance.AuthLevel != public {
defaultPolicy = getLevel(policy.DefaultPolicy)
}

ruleOthers := &AccessControlRule{
Position: position,
Policy: defaulPolicy,
Policy: defaultPolicy,
DefaultRule: true,
}
ruleAddResources(othersResources, ruleOthers)
ruleAddDomain(domains, ruleOthers)
if entrance.AuthLevel == "private" {
ruleAddDomain(localDomains, ruleOthers)
}

rules = append(rules, ruleOthers)

position++
appendRule(ruleOthers)

// add app root path to default policy with options.
ruleRoot := &AccessControlRule{
Position: position,
Policy: defaulPolicy,
Policy: defaultPolicy,
OneTimeValid: policy.OneTime,
ValidDuration: time.Duration(policy.Duration) * time.Second,
DefaultRule: true,
}
ruleAddDomain(domains, ruleRoot)
if entrance.AuthLevel == "private" {
ruleAddDomain(localDomains, ruleRoot)
}

rules = append(rules, ruleRoot)

appendRule(ruleRoot)
if entrance.AuthLevel == internal {
ruleOthersForLocal := &AccessControlRule{
Position: position,
Policy: NewLevel(public),
DefaultRule: true,
}
ruleAddDomain(localDomains, ruleOthersForLocal)
appendRule(ruleOthersForLocal)
}
}

return rules, nil
Expand Down

0 comments on commit be51fb9

Please sign in to comment.