Skip to content

Commit 2864caa

Browse files
authored
feat:支持初始化管理员帐户 & 支持新版的鉴权逻辑操作
1 parent 9505632 commit 2864caa

File tree

21 files changed

+467
-56
lines changed

21 files changed

+467
-56
lines changed

common/log/config.go

+19-27
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,29 @@
2323
// easy to build programs that use a consistent interface for logging. Here's an example
2424
// of a simple Cobra-based program using this log package:
2525
//
26-
// func main() {
27-
// // get the default logging options
28-
// options := log.DefaultOptions()
26+
// func main() {
27+
// // get the default logging options
28+
// options := log.DefaultOptions()
2929
//
30-
// rootCmd := &cobra.Command{
31-
// Run: func(cmd *cobra.Command, args []string) {
30+
// rootCmd := &cobra.Command{
31+
// Run: func(cmd *cobra.Command, args []string) {
3232
//
33-
// // configure the logging system
34-
// if err := log.Configure(options); err != nil {
35-
// // print an error and quit
36-
// }
33+
// // configure the logging system
34+
// if err := log.Configure(options); err != nil {
35+
// // print an error and quit
36+
// }
3737
//
38-
// // output some logs
39-
// log.Info("Hello")
40-
// log.Sync()
41-
// },
42-
// }
38+
// // output some logs
39+
// log.Info("Hello")
40+
// log.Sync()
41+
// },
42+
// }
4343
//
44-
// // add logging-specific flags to the cobra command
45-
// options.AttachCobraFlags(rootCmd)
46-
// rootCmd.SetArgs(os.Args[1:])
47-
// rootCmd.Execute()
48-
// }
44+
// // add logging-specific flags to the cobra command
45+
// options.AttachCobraFlags(rootCmd)
46+
// rootCmd.SetArgs(os.Args[1:])
47+
// rootCmd.Execute()
48+
// }
4949
//
5050
// Once configured, this package intercepts the output of the standard golang "log" package as well as anything
5151
// sent to the global zap logger (zap.L()).
@@ -61,8 +61,6 @@ import (
6161
"github.com/natefinch/lumberjack"
6262
"go.uber.org/zap"
6363
"go.uber.org/zap/zapcore"
64-
"go.uber.org/zap/zapgrpc"
65-
"google.golang.org/grpc/grpclog"
6664
)
6765

6866
// none is used to disable logging output as well as to disable stack tracing.
@@ -323,12 +321,6 @@ func Configure(options *Options) error {
323321

324322
// capture standard golang "log" package output and force it through our logger
325323
_ = zap.RedirectStdLog(captureLogger)
326-
327-
// capture gRPC logging
328-
if options.LogGrpc {
329-
grpclog.SetLogger(zapgrpc.NewLogger(captureLogger.WithOptions(zap.AddCallerSkip(2))))
330-
}
331-
332324
return nil
333325
}
334326

common/log/options.go

-6
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,6 @@ type Options struct {
109109
// JSONEncoding controls whether the log is formatted as JSON.
110110
JSONEncoding bool
111111

112-
// LogGrpc indicates that Grpc logs should be captured. The default is true.
113-
// This is not exposed through the command-line flags, as this flag is mainly useful for testing: Grpc
114-
// stack will hold on to the logger even though it gets closed. This causes data races.
115-
LogGrpc bool
116-
117112
Level string
118113

119114
outputLevels string
@@ -131,7 +126,6 @@ func DefaultOptions() *Options {
131126
RotationMaxBackups: defaultRotationMaxBackups,
132127
outputLevels: DefaultScopeName + ":" + levelToString[defaultOutputLevel],
133128
stackTraceLevels: DefaultScopeName + ":" + levelToString[defaultStackTraceLevel],
134-
LogGrpc: false,
135129
}
136130
}
137131

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/natefinch/lumberjack v2.0.0+incompatible
99
github.com/stretchr/testify v1.8.1
1010
go.uber.org/zap v1.13.0
11-
google.golang.org/grpc v1.50.1
11+
google.golang.org/grpc v1.51.0
1212
gopkg.in/yaml.v2 v2.4.0
1313
)
1414

@@ -36,6 +36,7 @@ require (
3636
github.com/kr/pretty v0.3.1 // indirect
3737
github.com/mattn/go-isatty v0.0.14 // indirect
3838
github.com/pkg/errors v0.9.1 // indirect
39+
github.com/polarismesh/specification v1.5.3
3940
go.uber.org/atomic v1.10.0 // indirect
4041
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
4142
golang.org/x/net v0.4.0 // indirect

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
9090
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
9191
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
9292
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
93+
github.com/polarismesh/specification v1.5.3 h1:KUzOtjYlUCC5qjWrwW1kI0wHPvwnwPqUIa8eWh3TJro=
94+
github.com/polarismesh/specification v1.5.3/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU=
9395
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
9496
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
9597
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
@@ -217,6 +219,7 @@ google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTp
217219
google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
218220
google.golang.org/grpc v1.50.1 h1:DS/BukOZWp8s6p4Dt/tOaJaTQyPyOoCcrjroHuCeLzY=
219221
google.golang.org/grpc v1.50.1/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
222+
google.golang.org/grpc v1.51.0/go.mod h1:wgNDFcnuBGmxLKI/qn4T+m5BtEBYXJPvibbUPsAIPww=
220223
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
221224
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
222225
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=

handlers/proxy.go

+55-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"io/ioutil"
2626
"net/http"
2727
"net/http/httputil"
28+
"sync"
2829
"time"
2930

3031
"github.com/gin-gonic/gin"
@@ -33,8 +34,54 @@ import (
3334

3435
"github.com/polarismesh/polaris-console/bootstrap"
3536
"github.com/polarismesh/polaris-console/common/log"
37+
"github.com/polarismesh/specification/source/go/api/v1/security"
3638
)
3739

40+
func NewAdminGetter(conf *bootstrap.Config) {
41+
_adminGetter.conf = conf
42+
}
43+
44+
var _adminGetter = &AdminUserGetter{}
45+
46+
type AdminUserGetter struct {
47+
conf *bootstrap.Config
48+
lock sync.RWMutex
49+
user *security.User
50+
}
51+
52+
func (a *AdminUserGetter) GetAdminInfo() (*security.User, error) {
53+
a.lock.Lock()
54+
defer a.lock.Unlock()
55+
56+
if a.user != nil {
57+
return a.user, nil
58+
}
59+
60+
resp, err := http.Get(fmt.Sprintf("http://%s/maintain/v1/mainuser/exist", a.conf.PolarisServer.Address))
61+
if err != nil {
62+
log.Error("[Proxy][Login] get admin info fail", zap.Error(err))
63+
return nil, err
64+
}
65+
66+
if resp.StatusCode != http.StatusOK {
67+
return nil, errors.New("get admin info fail")
68+
}
69+
70+
body, err := ioutil.ReadAll(resp.Body)
71+
if err != nil {
72+
log.Error("[Proxy][Login] get admin info fail", zap.Error(err))
73+
return nil, err
74+
}
75+
76+
user := &security.User{}
77+
if err = json.Unmarshal(body, user); err != nil {
78+
log.Error("[Proxy][Login] get admin info fail", zap.Error(err))
79+
return nil, err
80+
}
81+
a.user = user
82+
return a.user, nil
83+
}
84+
3885
// ServiceOwner 服务(规则)负责人信息
3986
type ServiceOwner struct {
4087
Namespace string
@@ -63,12 +110,16 @@ func ReverseProxyForLogin(polarisServer *bootstrap.PolarisServer, conf *bootstra
63110
log.Error("[Proxy][Login] modify login request fail", zap.Error(err))
64111
return
65112
}
113+
114+
admin, err := _adminGetter.GetAdminInfo()
115+
if err != nil {
116+
log.Error("[Proxy][Login] modify login request fail", zap.Error(err))
117+
return
118+
}
119+
66120
loginBody := &LoginRequest{}
67121
_ = json.Unmarshal(body, loginBody)
68-
loginBody.Owner = "polaris"
69-
if len(conf.WebServer.MainUser) != 0 {
70-
loginBody.Owner = conf.WebServer.MainUser
71-
}
122+
loginBody.Owner = admin.GetName().GetValue()
72123
body, err = json.Marshal(loginBody)
73124
if err != nil {
74125
log.Error("[Proxy][Login] modify login request fail", zap.Error(err))

main.go

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222

2323
"github.com/polarismesh/polaris-console/bootstrap"
24+
"github.com/polarismesh/polaris-console/handlers"
2425
"github.com/polarismesh/polaris-console/router"
2526
)
2627

@@ -32,6 +33,9 @@ func main() {
3233
fmt.Printf("[ERROR] loadConfig fail\n")
3334
return
3435
}
36+
37+
handlers.NewAdminGetter(config)
38+
3539
// 初始化相关配置
3640
bootstrap.Initialize(config)
3741
// 设置模式

router/admin_router.go

+2
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,7 @@ func AdminRouter(webSvr *gin.Engine, config *bootstrap.Config) {
4646
// 后端server路由组
4747
admin := webSvr.Group("/maintain/v1")
4848
admin.GET("/server/functions", handlers.ReverseProxyNoAuthForServer(&config.PolarisServer, config))
49+
admin.GET("/mainuser/exist", handlers.ReverseProxyNoAuthForServer(&config.PolarisServer, config))
50+
admin.POST("/mainuser/create", handlers.ReverseProxyNoAuthForServer(&config.PolarisServer, config))
4951
admin.GET("/bootstrap/config", handlers.ReverseProxyNoAuthForServer(&config.PolarisServer, config))
5052
}

web/src/index.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ import App from './app'
1111
import { Router, Switch, Route } from 'react-router-dom'
1212
import LoginPage from '@src/polaris/auth/login/Page'
1313
import LoginPageDuck from '@src/polaris/auth/login/PageDuck'
14+
import InitPage from '@src/polaris/auth/init/Page'
15+
import InitPageDuck from '@src/polaris/auth/init/PageDuck'
1416
import { connectWithDuck } from './polaris/common/helpers'
1517

1618
const Login = connectWithDuck(LoginPage, LoginPageDuck as any)
19+
const Init = connectWithDuck(InitPage, InitPageDuck as any)
1720
export default function render() {
1821
ReactDOM.unmountComponentAtNode(document.querySelector('#polaris-console'))
1922
ReactDOM.render(
2023
<Router history={history}>
2124
<Switch>
25+
<Route exact path='/init' component={Init} />
2226
<Route exact path='/login' component={Login} />
2327
<Route exact path='*'>
2428
<App />

web/src/polaris/administration/dynamicRoute/customRoute/getColumns.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,4 @@ export default (
135135
},
136136
},
137137
]
138-
}
138+
}

web/src/polaris/auth/init/Page.tsx

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import * as React from 'react'
2+
import { purify, DuckCmpProps } from 'saga-duck'
3+
4+
import Duck from './PageDuck'
5+
import insertCSS from '@src/polaris/common/helpers/insertCSS'
6+
import { Row, Col, Card, H2, Text, Form, Button, Input as TeaInput, Copy, Bubble } from 'tea-component'
7+
import FormField from '@src/polaris/common/duckComponents/form/Field'
8+
import Input from '@src/polaris/common/duckComponents/form/Input'
9+
insertCSS(
10+
'login',
11+
`.login-background{
12+
height:100vh;
13+
width:100vw;
14+
overflow:hidden;
15+
16+
}`,
17+
)
18+
export default purify(function (props: DuckCmpProps<Duck>) {
19+
const { duck, store, dispatch } = props
20+
const { ducks, creators, selector } = duck
21+
const { userName, password } = ducks.form.getAPI(store, dispatch).getFields(['userName', 'password'])
22+
const { preError } = selector(store)
23+
return (
24+
<div
25+
style={{ background: 'url(static/img/login-background.png)', backgroundSize: '100% 100%' }}
26+
className={'login-background'}
27+
>
28+
<img
29+
src={'static/img/logo-polaris.png'}
30+
style={{ width: '200px', position: 'absolute', top: 0, left: 0, padding: '15px' }}
31+
/>
32+
<Row style={{ margin: '30vh 0 30vh 0', height: '40vh' }}>
33+
<Col span={4}></Col>
34+
35+
<Col span={16}>
36+
<Row style={{ maxWidth: '1000px', margin: 'auto' }}>
37+
<Col span={15}>
38+
<Card
39+
style={{
40+
backgroundColor: 'transparent',
41+
border: 'none',
42+
padding: '5vh 0',
43+
}}
44+
bordered
45+
>
46+
<Card.Body>
47+
<Text parent={'div'} style={{ color: '#fff', fontSize: '24px', width: '550px' }}>
48+
北极星服务治理中心
49+
</Text>
50+
<Text parent={'div'} style={{ color: 'rgba(255, 255, 255, 0.6)', width: '450px' }}>
51+
一个支持多语言、多框架和异构基础设施的服务治理中心,提供服务发现、流量调度、熔断降级、限流鉴权和可观测性等服务治理功能。北极星治理中心默认提供服务注册功能,也可以搭配其他服务注册中心使用。
52+
</Text>
53+
</Card.Body>
54+
</Card>
55+
</Col>
56+
<Col span={9}>
57+
<Card>
58+
<Card.Body>
59+
<Row>
60+
<Col>
61+
<H2>
62+
<Text align={'center'} parent={'div'} style={{ width: '100%' }}>
63+
初始化主账户
64+
</Text>
65+
</H2>
66+
</Col>
67+
</Row>
68+
<Row>
69+
<Col>
70+
<Text theme={'weak'} parent={'div'} style={{ width: '100%' }} align={'center'}>
71+
外网访问建议设置访问控制策略
72+
</Text>
73+
</Col>
74+
</Row>
75+
<Form style={{ padding: '20px 0px' }}>
76+
<FormField field={userName} label={'用户名'}>
77+
<Input field={userName} size={'full'} />
78+
</FormField>
79+
<FormField field={password} label={'密码'}>
80+
<TeaInput.Password
81+
value={password.getValue() || ''}
82+
size={'full'}
83+
onChange={(v) => {
84+
password.setValue(v)
85+
password.setTouched(true)
86+
password.setError('')
87+
}}
88+
onPressEnter={() => {
89+
password.setError('')
90+
dispatch(creators.submit())
91+
}}
92+
rules={false}
93+
disabled={preError}
94+
/>
95+
</FormField>
96+
</Form>
97+
<Row>
98+
<Col span={8}></Col>
99+
<Bubble>
100+
<Col span={8}>
101+
<Button
102+
type={'primary'}
103+
style={{ width: '100%', margin: 'auto' }}
104+
onClick={() => {
105+
password.setError('')
106+
dispatch(creators.submit())
107+
}}
108+
disabled={preError}
109+
>
110+
初始化
111+
</Button>
112+
</Col>
113+
</Bubble>
114+
</Row>
115+
</Card.Body>
116+
</Card>
117+
</Col>
118+
</Row>
119+
</Col>
120+
<Col span={4}></Col>
121+
</Row>
122+
</div>
123+
)
124+
})

0 commit comments

Comments
 (0)