Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Add AKSK API [FUTURE Rel.] #1319

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
129 changes: 129 additions & 0 deletions client/aksk.go
Original file line number Diff line number Diff line change
@@ -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)
}

Binary file added cmd/osdsapiserver/osdsapiserver
Binary file not shown.
55 changes: 55 additions & 0 deletions pkg/api/controllers/aksk.go
Original file line number Diff line number Diff line change
@@ -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
}

}

32 changes: 32 additions & 0 deletions pkg/api/routers/aksk.go
Original file line number Diff line number Diff line change
@@ -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)
}
55 changes: 55 additions & 0 deletions pkg/model/aksk.go
Original file line number Diff line number Diff line change
@@ -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)
}

6 changes: 6 additions & 0 deletions pkg/utils/urls/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
16 changes: 16 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
package utils

import (
cryptorand "crypto/rand"
"encoding/json"
"errors"
"fmt"
"math/rand"
"math/big"
"os"
"reflect"
"sort"
Expand Down Expand Up @@ -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
}