Skip to content

Commit

Permalink
Merge pull request #4 from beclab/feat/internal-authlevel
Browse files Browse the repository at this point in the history
feat: add internal authlevel
  • Loading branch information
eball authored Sep 19, 2024
2 parents fb4d1c2 + 7303d09 commit 29dd3dd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 22 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
84 changes: 62 additions & 22 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,74 @@ 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 != internal {
ruleAddDomain(localDomains, ruleOthers)
}

appendRule(ruleOthers)

rules = append(rules, ruleOthers)
// if policy is internal, local and non-local must add two individual rules
if entrance.AuthLevel == internal {
ruleOthersLocal := &AccessControlRule{
Position: position,
Policy: defaultLocalPolicy,
DefaultRule: true,
}

position++
ruleAddResources(othersResources, ruleOthersLocal)
ruleAddDomain(localDomains, ruleOthersLocal)
appendRule(ruleOthersLocal)
}

// 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 != internal {
ruleAddDomain(localDomains, ruleRoot)
}

appendRule(ruleRoot)

rules = append(rules, ruleRoot)
// if policy is internal, local and non-local must add two individual rules
if entrance.AuthLevel == internal {
ruleRootLocal := &AccessControlRule{
Position: position,
Policy: defaultLocalPolicy,
OneTimeValid: policy.OneTime,
ValidDuration: time.Duration(policy.Duration) * time.Second,
DefaultRule: true,
}

ruleAddDomain(localDomains, ruleRootLocal)
appendRule(ruleRootLocal)
}
}

return rules, nil
Expand Down

0 comments on commit 29dd3dd

Please sign in to comment.