2023-03-19 21:42:21 +00:00
|
|
|
// Copyright 2023 Woodpecker 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.
|
|
|
|
|
2023-01-28 13:13:04 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-08 16:09:34 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-01-28 13:13:04 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc/proto"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store/types"
|
2023-01-28 13:13:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type WoodpeckerAuthServer struct {
|
|
|
|
proto.UnimplementedWoodpeckerAuthServer
|
|
|
|
jwtManager *JWTManager
|
|
|
|
agentMasterToken string
|
|
|
|
store store.Store
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWoodpeckerAuthServer(jwtManager *JWTManager, agentMasterToken string, store store.Store) *WoodpeckerAuthServer {
|
|
|
|
return &WoodpeckerAuthServer{jwtManager: jwtManager, agentMasterToken: agentMasterToken, store: store}
|
|
|
|
}
|
|
|
|
|
2023-03-19 21:42:21 +00:00
|
|
|
func (s *WoodpeckerAuthServer) Auth(_ context.Context, req *proto.AuthRequest) (*proto.AuthResponse, error) {
|
2023-03-18 19:35:27 +00:00
|
|
|
agent, err := s.getAgent(req.AgentId, req.AgentToken)
|
2023-01-28 13:13:04 +00:00
|
|
|
if err != nil {
|
2024-01-10 21:56:42 +00:00
|
|
|
return nil, fmt.Errorf("agent could not auth: %w", err)
|
2023-01-28 13:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
accessToken, err := s.jwtManager.Generate(agent.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-19 21:42:21 +00:00
|
|
|
return &proto.AuthResponse{
|
2023-01-28 13:13:04 +00:00
|
|
|
Status: "ok",
|
|
|
|
AgentId: agent.ID,
|
|
|
|
AccessToken: accessToken,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-03-18 19:35:27 +00:00
|
|
|
func (s *WoodpeckerAuthServer) getAgent(agentID int64, agentToken string) (*model.Agent, error) {
|
2023-07-08 16:09:34 +00:00
|
|
|
// global agent secret auth
|
|
|
|
if s.agentMasterToken != "" {
|
|
|
|
if agentToken == s.agentMasterToken && agentID == -1 {
|
|
|
|
agent := new(model.Agent)
|
|
|
|
agent.Name = ""
|
|
|
|
agent.OwnerID = -1 // system agent
|
|
|
|
agent.Token = s.agentMasterToken
|
|
|
|
agent.Backend = ""
|
|
|
|
agent.Platform = ""
|
|
|
|
agent.Capacity = -1
|
|
|
|
err := s.store.AgentCreate(agent)
|
|
|
|
if err != nil {
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Error().Err(err).Msg("error creating system agent")
|
2023-07-08 16:09:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agent, nil
|
2023-01-28 13:13:04 +00:00
|
|
|
}
|
|
|
|
|
2023-07-08 16:09:34 +00:00
|
|
|
if agentToken == s.agentMasterToken {
|
|
|
|
agent, err := s.store.AgentFind(agentID)
|
|
|
|
if err != nil && errors.Is(err, types.RecordNotExist) {
|
|
|
|
return nil, fmt.Errorf("AgentID not found in database")
|
|
|
|
}
|
|
|
|
return agent, err
|
|
|
|
}
|
2023-01-28 13:13:04 +00:00
|
|
|
}
|
|
|
|
|
2023-07-08 16:09:34 +00:00
|
|
|
// individual agent token auth
|
|
|
|
agent, err := s.store.AgentFindByToken(agentToken)
|
|
|
|
if err != nil && errors.Is(err, types.RecordNotExist) {
|
|
|
|
return nil, fmt.Errorf("individual agent not found by token: %w", err)
|
|
|
|
}
|
|
|
|
return agent, err
|
2023-01-28 13:13:04 +00:00
|
|
|
}
|