Skip to content

Commit

Permalink
fix: cloudaccount is created as system shared by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiu Jian committed Apr 20, 2020
1 parent 76ee769 commit e0fc57b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 30 deletions.
13 changes: 5 additions & 8 deletions pkg/apigateway/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package handler

import (
"context"
"encoding/base64"
"fmt"
"net/http"
"strings"
Expand Down Expand Up @@ -269,6 +270,10 @@ func (h *AuthHandlers) doCredentialLogin(ctx context.Context, req *http.Request,
if err != nil {
return nil, httperrors.NewInputParameterError("get password in body")
}
// try base64 decryption
if decPasswd, err := base64.StdEncoding.DecodeString(passwd); err == nil {
passwd = string(decPasswd)
}
if len(uname) == 0 || len(passwd) == 0 {
return nil, httperrors.NewInputParameterError("username or password is empty")
}
Expand Down Expand Up @@ -533,14 +538,6 @@ func FetchRegion(req *http.Request) string {
return ""
}

func fetchDomain(req *http.Request) string {
r, e := req.Cookie("domain")
if e != nil {
return ""
}
return r.Value
}

type role struct {
id string
name string
Expand Down
7 changes: 4 additions & 3 deletions pkg/apis/compute/cloudaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,11 @@ type CloudaccountCreateInput struct {
IsOnPremise bool

// 指定云账号所属的项目
Tenant string `json:"tenant"`

// Tenant string `json:"tenant"`
// swagger:ignore
TenantId string
// TenantId string

apis.ProjectizedResourceInput

// 启用自动同步
// default: false
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/identity/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

type IdentityBaseResourceCreateInput struct {
apis.StandaloneResourceCreateInput
apis.DomainizedResourceCreateInput
}

type EnabledIdentityBaseResourceCreateInput struct {
Expand Down
16 changes: 7 additions & 9 deletions pkg/apis/identity/zz_generated.model.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions pkg/apis/monitor/zz_generated.model.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions pkg/cloudcommon/db/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package db

import (
"context"
"database/sql"

"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/reflectutils"
"yunion.io/x/sqlchemy"

Expand Down Expand Up @@ -163,3 +165,16 @@ func (manager *SDomainizedResourceBaseManager) FetchCustomizeColumns(
}
return ret
}

func ValidateDomainizedResourceInput(ctx context.Context, input apis.DomainizedResourceInput) (*STenant, apis.DomainizedResourceInput, error) {
domain, err := DefaultDomainFetcher(ctx, input.ProjectDomain)
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return nil, input, httperrors.NewResourceNotFoundError2("domain", input.ProjectDomain)
} else {
return nil, input, errors.Wrap(err, "TenantCacheManager.FetchDomainByIdOrName")
}
}
input.ProjectDomain = domain.GetId()
return domain, input, nil
}
14 changes: 14 additions & 0 deletions pkg/cloudcommon/db/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package db

import (
"context"
"database/sql"
"time"

"yunion.io/x/jsonutils"
Expand Down Expand Up @@ -197,3 +198,16 @@ func fetchProjects(ctx context.Context, projectIds []string, isDomain bool) map[
}
return ret
}

func ValidateProjectizedResourceInput(ctx context.Context, input apis.ProjectizedResourceInput) (*STenant, apis.ProjectizedResourceInput, error) {
tenant, err := DefaultProjectFetcher(ctx, input.Project)
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return nil, input, httperrors.NewResourceNotFoundError2("project", input.Project)
} else {
return nil, input, errors.Wrap(err, "TenantCacheManager.FetchTenantByIdOrName")
}
}
input.Project = tenant.GetId()
return tenant, input, nil
}
15 changes: 8 additions & 7 deletions pkg/compute/models/cloudaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,11 @@ func (manager *SCloudaccountManager) ValidateCreateData(
return input, err
}

if len(input.Tenant) > 0 {
tenantObj, err := db.TenantCacheManager.FetchTenantByIdOrName(ctx, input.Tenant)
if len(input.Project) > 0 {
_, input.ProjectizedResourceInput, err = db.ValidateProjectizedResourceInput(ctx, input.ProjectizedResourceInput)
if err != nil {
if err == sql.ErrNoRows {
return input, httperrors.NewResourceNotFoundError("failed to found tenant %s", input.Tenant)
}
return input, httperrors.NewGeneralError(errors.Wrap(err, "FetchTenantByIdOrName"))
return input, errors.Wrap(err, "db.ValidateProjectizedResourceInput")
}
input.TenantId = tenantObj.GetId()
}

if !cloudprovider.IsSupported(input.Provider) {
Expand Down Expand Up @@ -474,7 +470,12 @@ func (self *SCloudaccount) CustomizeCreate(ctx context.Context, userCred mcclien
}
self.DomainId = ownerId.GetProjectDomainId()
// self.EnableAutoSync = false
// force private and share_mode=account_domain
self.ShareMode = api.CLOUD_ACCOUNT_SHARE_MODE_ACCOUNT_DOMAIN
self.IsPublic = false
self.PublicScope = string(rbacutils.ScopeNone)
// mark the public_scope has been set
data.(*jsonutils.JSONDict).Set("public_scope", jsonutils.NewString(self.PublicScope))
return self.SEnabledStatusInfrasResourceBase.CustomizeCreate(ctx, userCred, ownerId, query, data)
}

Expand Down

0 comments on commit e0fc57b

Please sign in to comment.