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

feat: core-api add public key api #392

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core-api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ core
*.log
bk-apigateway-core-api
.coverage.cov
config.yaml
2 changes: 1 addition & 1 deletion src/core-api/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"fmt"

"github.com/getsentry/raven-go"
sentry "github.com/getsentry/sentry-go"
"github.com/getsentry/sentry-go"
"github.com/spf13/viper"

"core/pkg/config"
Expand Down
5 changes: 3 additions & 2 deletions src/core-api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ require (
github.com/TencentBlueKing/gopkg v1.2.0
github.com/agiledragon/gomonkey/v2 v2.10.1
github.com/dlmiddlecote/sqlstats v1.0.2
github.com/getsentry/raven-go v0.2.0
github.com/getsentry/sentry-go v0.20.0
github.com/gin-gonic/contrib v0.0.0-20221130124618-7e01895a63f2
github.com/gin-gonic/gin v1.8.2
github.com/go-playground/validator/v10 v10.11.1
github.com/go-sql-driver/mysql v1.7.0
github.com/gofrs/uuid v4.4.0+incompatible
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/golang/mock v1.4.4
github.com/jmoiron/sqlx v1.3.5
github.com/json-iterator/go v1.1.12
github.com/onsi/ginkgo/v2 v2.9.5
github.com/onsi/gomega v1.27.6
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/prometheus/client_golang v1.15.0
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.7.0
Expand All @@ -44,7 +47,6 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/getsentry/raven-go v0.2.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -65,7 +67,6 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions src/core-api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE=
Expand Down
102 changes: 102 additions & 0 deletions src/core-api/pkg/api/open/public_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* TencentBlueKing is pleased to support the open source community by making
* 蓝鲸智云 - API 网关(BlueKing - APIGateway) available.
* Copyright (C) 2017 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.
*
* We undertake not to change the open source license (MIT license) applicable
* to the current version of the project delivered to anyone in the future.
*/

package open

import (
"database/sql"
"errors"
"net/http"

"github.com/gin-gonic/gin"

"core/pkg/constant"
"core/pkg/service"
"core/pkg/util"
)

type queryPublicKeySerializer struct {
BkGatewayName string `uri:"gateway_name" binding:"required" example:"gateway"`
}

// QueryPublicKeyV1 will query the public key for gateway from database
func QueryPublicKeyV1(c *gin.Context) {
// uri params: bk_gateway_name or api_name
// response body:
// {
// "data": {
// "public_key": ""
// }
// }
Han-Ya-Jun marked this conversation as resolved.
Show resolved Hide resolved

var query queryPublicKeySerializer
if err := c.ShouldBindUri(&query); err != nil {
util.LegacyErrorJSONResponse(c, util.BadRequestError, http.StatusBadRequest, err.Error())
return
}
svc := service.NewGatewayPublicKeyService()

publicKey, err := svc.GetByGatewayName(c.Request.Context(), query.BkGatewayName)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
util.LegacyErrorJSONResponse(c, util.NotFoundError, http.StatusNotFound, err.Error())
return
} else {
util.LegacyErrorJSONResponse(c, util.SystemError, http.StatusInternalServerError, err.Error())
return
}
}
Han-Ya-Jun marked this conversation as resolved.
Show resolved Hide resolved
util.LegacySuccessJsonResponse(c, gin.H{
"issuer": c.GetString(constant.BkGatewayJWTIssuerKey),
"public_key": publicKey,
})
}

// QueryPublicKeyV2 will query the public key for gateway from database
func QueryPublicKeyV2(c *gin.Context) {
// uri params: bk_gateway_name or api_name
// response body:
// {
// "data": {
// "public_key": ""
// }
// }

var query queryPublicKeySerializer
if err := c.ShouldBindUri(&query); err != nil {
util.BadRequestErrorJSONResponse(c, util.ValidationErrorMessage(err))
return
}
svc := service.NewGatewayPublicKeyService()

publicKey, err := svc.GetByGatewayName(c.Request.Context(), query.BkGatewayName)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
util.NotFoundJSONResponse(c, err.Error())
return
} else {
util.SystemErrorJSONResponse(c, err)
return
}
}

util.SuccessJSONResponse(c, gin.H{
"issuer": c.GetString(constant.BkGatewayJWTIssuerKey),
"public_key": publicKey,
})
}
2 changes: 0 additions & 2 deletions src/core-api/pkg/cacheimpls/publish_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func TestPublishEvent_Key(t *testing.T) {
}

func TestPublishEventExistsAndSet(t *testing.T) {

key := PublishEventKey{
GatewayID: 1,
StageID: 2,
Expand All @@ -53,5 +52,4 @@ func TestPublishEventExistsAndSet(t *testing.T) {

exists = PublishEventExists(context.Background(), key)
assert.Equal(t, true, exists)

}
21 changes: 21 additions & 0 deletions src/core-api/pkg/constant/open.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* TencentBlueKing is pleased to support the open source community by making
* 蓝鲸智云 - API 网关(BlueKing - APIGateway) available.
* Copyright (C) 2017 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.
*
* We undertake not to change the open source license (MIT license) applicable
* to the current version of the project delivered to anyone in the future.
*/

package constant

const BkGatewayJWTIssuerKey = "bk_gateway_jwt_issuer"
Loading