2022-02-02 12:27:49 +00:00
|
|
|
package pogs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
capnp "zombiezen.com/go/capnproto2"
|
|
|
|
"zombiezen.com/go/capnproto2/rpc"
|
|
|
|
"zombiezen.com/go/capnproto2/server"
|
2024-05-14 04:22:06 +00:00
|
|
|
|
2024-05-28 21:14:25 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc/metrics"
|
2024-05-14 04:22:06 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc/proto"
|
2022-02-02 12:27:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ConfigurationManager interface {
|
2024-05-28 21:14:25 +00:00
|
|
|
// UpdateConfiguration is the call provided to cloudflared to load the latest remote configuration.
|
2022-02-11 15:46:04 +00:00
|
|
|
UpdateConfiguration(ctx context.Context, version int32, config []byte) *UpdateConfigurationResponse
|
2022-02-02 12:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ConfigurationManager_PogsImpl struct {
|
|
|
|
impl ConfigurationManager
|
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func ConfigurationManager_ServerToClient(c ConfigurationManager) proto.ConfigurationManager {
|
|
|
|
return proto.ConfigurationManager_ServerToClient(ConfigurationManager_PogsImpl{c})
|
2022-02-02 12:27:49 +00:00
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func (i ConfigurationManager_PogsImpl) UpdateConfiguration(p proto.ConfigurationManager_updateConfiguration) error {
|
2024-05-28 21:14:25 +00:00
|
|
|
return metrics.ObserveServerHandler(func() error { return i.updateConfiguration(p) }, metrics.ConfigurationManager, metrics.OperationUpdateConfiguration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i ConfigurationManager_PogsImpl) updateConfiguration(p proto.ConfigurationManager_updateConfiguration) error {
|
2022-02-02 12:27:49 +00:00
|
|
|
server.Ack(p.Options)
|
|
|
|
|
|
|
|
version := p.Params.Version()
|
|
|
|
config, err := p.Params.Config()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := p.Results.NewResult()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-11 15:46:04 +00:00
|
|
|
updateResp := i.impl.UpdateConfiguration(p.Ctx, version, config)
|
2022-02-02 12:27:49 +00:00
|
|
|
return updateResp.Marshal(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConfigurationManager_PogsClient struct {
|
|
|
|
Client capnp.Client
|
|
|
|
Conn *rpc.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c ConfigurationManager_PogsClient) Close() error {
|
|
|
|
c.Client.Close()
|
|
|
|
return c.Conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c ConfigurationManager_PogsClient) UpdateConfiguration(ctx context.Context, version int32, config []byte) (*UpdateConfigurationResponse, error) {
|
2024-05-14 04:22:06 +00:00
|
|
|
client := proto.ConfigurationManager{Client: c.Client}
|
|
|
|
promise := client.UpdateConfiguration(ctx, func(p proto.ConfigurationManager_updateConfiguration_Params) error {
|
2022-02-02 12:27:49 +00:00
|
|
|
p.SetVersion(version)
|
|
|
|
return p.SetConfig(config)
|
|
|
|
})
|
|
|
|
result, err := promise.Result().Struct()
|
|
|
|
if err != nil {
|
|
|
|
return nil, wrapRPCError(err)
|
|
|
|
}
|
|
|
|
response := new(UpdateConfigurationResponse)
|
|
|
|
|
|
|
|
err = response.Unmarshal(result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateConfigurationResponse struct {
|
|
|
|
LastAppliedVersion int32 `json:"lastAppliedVersion"`
|
|
|
|
Err error `json:"err"`
|
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func (p *UpdateConfigurationResponse) Marshal(s proto.UpdateConfigurationResponse) error {
|
2022-02-02 12:27:49 +00:00
|
|
|
s.SetLatestAppliedVersion(p.LastAppliedVersion)
|
|
|
|
if p.Err != nil {
|
|
|
|
return s.SetErr(p.Err.Error())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func (p *UpdateConfigurationResponse) Unmarshal(s proto.UpdateConfigurationResponse) error {
|
2022-02-02 12:27:49 +00:00
|
|
|
p.LastAppliedVersion = s.LatestAppliedVersion()
|
|
|
|
respErr, err := s.Err()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if respErr != "" {
|
|
|
|
p.Err = fmt.Errorf(respErr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|