Skip to content

Commit

Permalink
feat: bkbase api 增加 app_code 和 space_uid 参数以及使用 body 鉴权 --story=11946…
Browse files Browse the repository at this point in the history
…2756 (#516)
  • Loading branch information
shamcleren authored Sep 4, 2024
1 parent 92a2f6f commit 0ebed11
Show file tree
Hide file tree
Showing 25 changed files with 582 additions and 883 deletions.
47 changes: 28 additions & 19 deletions pkg/unify-query/bkapi/bkapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package bkapi

import (
"encoding/json"
"fmt"
"sync"

Expand All @@ -18,45 +19,53 @@ import (

const (
AdminUserName = "admin"
BkApiAuthorization = "X-Bkapi-Authorization"
BkAPIAuthorization = "X-Bkapi-Authorization"

BkUserNameKey = "bk_username"
BkAppCodeKey = "bk_app_code"
BkSecretKey = "bk_app_secret"
)

type BkApi struct {
type BkAPI struct {
address string

code string
secret string
authConfig map[string]string
}

var (
onceBkApi sync.Once
defaultBkApi *BkApi
onceBkAPI sync.Once
defaultBkAPI *BkAPI
)

func GetBkApi() *BkApi {
onceBkApi.Do(func() {
defaultBkApi = &BkApi{
address: viper.GetString(BkApiAddressConfigPath),
code: viper.GetString(BkApiCodeConfigPath),
secret: viper.GetString(BkApiSecretConfigPath),
func GetBkAPI() *BkAPI {
onceBkAPI.Do(func() {
defaultBkAPI = &BkAPI{
address: viper.GetString(BkAPIAddressConfigPath),
authConfig: map[string]string{
BkAppCodeKey: viper.GetString(BkAPICodeConfigPath),
BkSecretKey: viper.GetString(BkAPISecretConfigPath),
BkUserNameKey: AdminUserName,
},
}
})

return defaultBkApi
return defaultBkAPI
}

func (i *BkAPI) GetCode() string {
return i.authConfig[BkAppCodeKey]
}

func (i *BkApi) Headers(headers map[string]string) map[string]string {
func (i *BkAPI) Headers(headers map[string]string) map[string]string {
if len(headers) == 0 {
headers = make(map[string]string)
}
headers[BkApiAuthorization] = fmt.Sprintf(
`{"bk_username": "%s", "bk_app_code": "%s", "bk_app_secret": "%s"}`,
AdminUserName, i.code, i.secret,
)
auth, _ := json.Marshal(i.authConfig)
headers[BkAPIAuthorization] = string(auth)
return headers
}

func (i *BkApi) Url(path string) string {
func (i *BkAPI) Url(path string) string {
url := i.address
if path != "" {
url = fmt.Sprintf("%s/%s", i.address, path)
Expand Down
70 changes: 36 additions & 34 deletions pkg/unify-query/bkapi/bkdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
package bkapi

import (
"encoding/json"
"fmt"
"net/http"
"sync"

"github.com/spf13/viper"
Expand All @@ -22,69 +22,71 @@ const (

QuerySync = "query_sync"
QueryAsync = "query_async"

BkDataAuthenticationMethodKey = "bkdata_authentication_method"
BkDataDataTokenKey = "bkdata_data_token"
)

var (
onceBkDataApi sync.Once
defaultBkDataApi *BkDataApi
onceBkDataAPI sync.Once
defaultBkDataAPI *BkDataAPI
)

type BkDataApi struct {
bkApi *BkApi
type BkDataAPI struct {
bkAPI *BkAPI

uriPath string

uriPath string
token string
authenticationMethod string
authConfig map[string]string
}

func GetBkDataApi() *BkDataApi {
onceBkDataApi.Do(func() {
defaultBkDataApi = &BkDataApi{
bkApi: GetBkApi(),
token: viper.GetString(BkDataTokenConfigPath),
authenticationMethod: viper.GetString(BkDataAuthenticationMethodConfigPath),
uriPath: viper.GetString(BkDataUriPathConfigPath),
func GetBkDataAPI() *BkDataAPI {
onceBkDataAPI.Do(func() {
bkAPI := GetBkAPI()
defaultBkDataAPI = &BkDataAPI{
bkAPI: bkAPI,
uriPath: viper.GetString(BkDataUriPathConfigPath),
authConfig: map[string]string{
BkDataDataTokenKey: viper.GetString(BkDataTokenConfigPath),
BkDataAuthenticationMethodKey: viper.GetString(BkDataAuthenticationMethodConfigPath),
BkUserNameKey: AdminUserName,
BkAppCodeKey: bkAPI.GetCode(),
},
}
})
return defaultBkDataApi
return defaultBkDataAPI
}

func (i *BkDataApi) HttpHeaders(headers map[string]string) http.Header {
headers = i.Headers(headers)
netHeaders := make(http.Header, len(headers))
for k, v := range headers {
netHeaders[k] = []string{v}
}
return netHeaders
func (i *BkDataAPI) GetDataAuth() map[string]string {
return i.authConfig
}

func (i *BkDataApi) Headers(headers map[string]string) map[string]string {
func (i *BkDataAPI) Headers(headers map[string]string) map[string]string {
if len(headers) == 0 {
headers = make(map[string]string)
}
headers[BkDataAuthorization] = fmt.Sprintf(
`{"bkdata_authentication_method": "%s", "bkdata_data_token": "%s", "bk_username": "%s"}`,
i.authenticationMethod, i.token, AdminUserName,
)
return i.bkApi.Headers(headers)

auth, _ := json.Marshal(i.authConfig)
headers[BkDataAuthorization] = string(auth)
return i.bkAPI.Headers(headers)
}

func (i *BkDataApi) url(path string) string {
url := i.bkApi.Url(i.uriPath)
func (i *BkDataAPI) url(path string) string {
url := i.bkAPI.Url(i.uriPath)
if path != "" {
url = fmt.Sprintf("%s/%s", url, path)
}
return url
}

func (i *BkDataApi) QueryAsyncUrl() string {
func (i *BkDataAPI) QueryAsyncUrl() string {
return i.url(QueryAsync)
}

func (i *BkDataApi) QuerySyncUrl() string {
func (i *BkDataAPI) QuerySyncUrl() string {
return i.url(QuerySync)
}

func (i *BkDataApi) QueryEsUrl() string {
func (i *BkDataAPI) QueryEsUrl() string {
return fmt.Sprintf("%s/es", i.QuerySyncUrl())
}
25 changes: 25 additions & 0 deletions pkg/unify-query/bkapi/bkdata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Tencent is pleased to support the open source community by making
// 蓝鲸智云 - 监控平台 (BlueKing - Monitor) available.
// Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
// Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://opensource.org/licenses/MIT
// 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 bkapi

import (
"fmt"
"testing"

"github.com/TencentBlueKing/bkmonitor-datalink/pkg/unify-query/mock"
)

func TestGetDataAuth(t *testing.T) {
mock.Path = `../dist/local/unify-query.yaml`
mock.Init()

headers := GetBkDataAPI().Headers(nil)
fmt.Println(headers)
}
6 changes: 3 additions & 3 deletions pkg/unify-query/bkapi/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
BkDataTokenConfigPath = "bk_data.token"

// BKAPI 配置
BkApiAddressConfigPath = "bk_api.address"
BkApiCodeConfigPath = "bk_api.code"
BkApiSecretConfigPath = "bk_api.secret"
BkAPIAddressConfigPath = "bk_api.address"
BkAPICodeConfigPath = "bk_api.code"
BkAPISecretConfigPath = "bk_api.secret"
)
Loading

0 comments on commit 0ebed11

Please sign in to comment.