diff --git a/client/aksk.go b/client/aksk.go new file mode 100644 index 000000000..496ec9946 --- /dev/null +++ b/client/aksk.go @@ -0,0 +1,129 @@ +// Copyright 2021 The SODA Foundation Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package client + +import ( + "github.com/sodafoundation/api/pkg/model" + "github.com/sodafoundation/api/pkg/utils/urls" + "strings" +) + +// AkSkBuilder contains request body of handling a AkSk request. +type AkSkBuilder *model.AkSkSpec + +// NewAkSkMgr implementation +func NewAkSkMgr(r Receiver, edp string, tenantID string) *AkSkMgr { + return &AkSkMgr{ + Receiver: r, + Endpoint: edp, + TenantID: tenantID, + } +} + +// AkSk implementation +type AkSkMgr struct { + Receiver + Endpoint string + TenantID string +} + +/*type credentials struct { + Blob int `json:"blob"` + ProjectId string `json:"project_Id"` + CredentialsType string `json:"type"` + UserId string `json:"user_Id"` +} + + +func createAkSk(param,options){ +} + +func deleteAkSk(id,options){ +} + +func downloadAkSk(id,options){ +} + + +func getAkSkList(){ +} + +func addKey(projectId string, userId string){ +} +*/ + + +// CreateAkSk implementation +func (h *AkSkMgr) CreateAkSk(body AkSkBuilder) (*model.AkSkSpec, error) { + var res model.AkSkSpec + + url := strings.Join([]string{ + h.Endpoint, + urls.GenerateAkSkURL(urls.Client, h.TenantID)}, "/") + + if err := h.Recv(url, "POST", body, &res); + err != nil { + return nil, err + } + + return &res, nil +} + +// GetAkSk implementation +func (h *AkSkMgr) GetAkSk(ID string) (*model.AkSkSpec, error) { + var res model.AkSkSpec + url := strings.Join([]string{ + h.Endpoint, + urls.GenerateAkSkURL(urls.Client, h.TenantID, ID)}, "/") + + if err := h.Recv(url, "GET", nil, &res); err != nil { + return nil, err + } + + return &res, nil +} + +// ListAkSks implementation +func (h *AkSkMgr) ListAkSks(args ...interface{}) ([]*model.AkSkSpec, error) { + url := strings.Join([]string{ + h.Endpoint, + urls.GenerateAkSkURL(urls.Client, h.TenantID)}, "/") + + param, err := processListParam(args) + if err != nil { + return nil, err + } + + if param != "" { + url += "?" + param + } + + var res []*model.AkSkSpec + if err := h.Recv(url, "GET", nil, &res); err != nil { + return nil, err + } + return res, nil +} + + +// DeleteAkSk implementation +func (h *AkSkMgr) DeleteAkSk(ID string) error { + url := strings.Join([]string{ + h.Endpoint, + urls.GenerateAkSkURL(urls.Client, h.TenantID, ID)}, "/") + + return h.Recv(url, "DELETE", nil, nil) +} + diff --git a/cmd/osdsapiserver/osdsapiserver b/cmd/osdsapiserver/osdsapiserver new file mode 100755 index 000000000..c8510fc19 Binary files /dev/null and b/cmd/osdsapiserver/osdsapiserver differ diff --git a/pkg/api/controllers/aksk.go b/pkg/api/controllers/aksk.go new file mode 100644 index 000000000..d56c45f79 --- /dev/null +++ b/pkg/api/controllers/aksk.go @@ -0,0 +1,55 @@ +// Copyright 2021 The SODA Foundation Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package controllers + +import ( + "github.com/sodafoundation/api/pkg/api/policy" +) + +type AkSkPortal struct { + BasePortal +} + +func NewAkSkPortal() *AkSkPortal { + return &AkSkPortal{} +} + +func (p *AkSkPortal) ListAkSks() { + if !policy.Authorize(p.Ctx, "AkSk:list") { + return + } + +} + +func (p *AkSkPortal) CreateAkSk() { + if !policy.Authorize(p.Ctx, "AkSk:create") { + return + } + +} + +func (p *AkSkPortal) GetAkSk() { + if !policy.Authorize(p.Ctx, "AkSk:get") { + return + } +} + +func (p *AkSkPortal) DeleteAkSk() { + if !policy.Authorize(p.Ctx, "AkSk:delete") { + return + } + +} + diff --git a/pkg/api/routers/aksk.go b/pkg/api/routers/aksk.go new file mode 100644 index 000000000..250ed3c7e --- /dev/null +++ b/pkg/api/routers/aksk.go @@ -0,0 +1,32 @@ +// Copyright 2021 The SODA Foundation Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package routers + +import ( + "github.com/astaxie/beego" + "github.com/sodafoundation/api/pkg/api/controllers" + "github.com/sodafoundation/api/pkg/utils/constants" +) + +func init() { + + // add router for aksk api + akskns := + beego.NewNamespace("/"+constants.APIVersion+"/:tenantId/aksk", + beego.NSRouter("/aksks", controllers.NewAkSkPortal(), "post:CreateAkSk;get:ListAkSks"), + beego.NSRouter("/aksks/:UserId", controllers.NewAkSkPortal(), "get:GetAkSk;delete:DeleteAkSk"), + ) + beego.AddNamespace(akskns) +} diff --git a/pkg/model/aksk.go b/pkg/model/aksk.go new file mode 100644 index 000000000..f6667332b --- /dev/null +++ b/pkg/model/aksk.go @@ -0,0 +1,55 @@ +// Copyright 2021 The SODA Foundation Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package model + +import "encoding/json" + +//AkSkSpec is used to hold the data to generate the AccessKey and SecretKey for the User. + +type AkSkSpec struct { + *BaseModel + + // ProjectId or TenantId is the tenant that the user belongs to. + ProjectId string `json:"project_id,omitempty"` + + // The id of the user for whom the AkSk is being generated. + UserId string `json:"user_id,omitempty"` + + // The json containing the accesskey and secretkey + Blob string `json:"blob,omitempty"` + + // TODO: Confirm the usage of the Type field. + //The type of backend ?? + Type string `json:"type,omitempty"` +} + +// Blob to hold the access key and secret key. +type Blob struct { + + // Access key + Access string `json:"accessKey,omitempty"` + + // Secret key + Secret string `json:"secretKey ,omitempty"` +} + +// TODO : Check if required or not. +// MarshalJSON to remove sensitive data +func (m AkSkSpec) MarshalJSON() ([]byte, error) { + type akskResp AkSkSpec + resp := akskResp(m) + return json.Marshal(resp) +} + diff --git a/pkg/utils/urls/urls.go b/pkg/utils/urls/urls.go index 4453118c3..20e4442d0 100644 --- a/pkg/utils/urls/urls.go +++ b/pkg/utils/urls/urls.go @@ -80,6 +80,12 @@ func GenerateVolumeGroupURL(urlType int, tenantId string, in ...string) string { return generateURL("block/volumeGroups", urlType, tenantId, in...) } +// GenerateAkSkURL +func GenerateAkSkURL(urlType int, tenantId string, in ...string) string { + return generateURL("/v3/credentials", urlType, tenantId, in...) +} + + func generateURL(resource string, urlType int, tenantId string, in ...string) string { // If project id is not specified, ignore it. if tenantId == "" { diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 535895bda..9d7313fe4 100755 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -15,10 +15,12 @@ package utils import ( + cryptorand "crypto/rand" "encoding/json" "errors" "fmt" "math/rand" + "math/big" "os" "reflect" "sort" @@ -336,3 +338,17 @@ func ContainsIgnoreCase(a []string, x string) bool { } return false } + +func GenerateRandomString(n int) (string, error) { + const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-" + ret := make([]byte, n) + for i := 0; i < n; i++ { + num, err := cryptorand.Int(cryptorand.Reader, big.NewInt(int64(len(letters)))) + if err != nil { + return "", err + } + ret = append(ret, letters[num.Int64()]) + } + + return string(ret), nil +}