Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ services:
- /etc/timezone:/etc/timezone:ro
- ./data:/app/pb_data
restart: unless-stopped
extra_hosts:
- "host.docker.internal:host-gateway"
71 changes: 38 additions & 33 deletions internal/certapply/applicators/sp_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,46 @@ import (
)

func init() {
if err := ACMEHttp01Registries.Register(domain.ACMEHttp01ProviderTypeSSH, func(options *ProviderFactoryOptions) (challenge.Provider, error) {
credentials := domain.AccessConfigForSSH{}
if err := xmaps.Populate(options.ProviderAccessConfig, &credentials); err != nil {
return nil, fmt.Errorf("failed to populate provider access config: %w", err)
}
register := func(providerType domain.ACMEHttp01ProviderType) {
if err := ACMEHttp01Registries.Register(providerType, func(options *ProviderFactoryOptions) (challenge.Provider, error) {
credentials := domain.AccessConfigForSSH{}
if err := xmaps.Populate(options.ProviderAccessConfig, &credentials); err != nil {
return nil, fmt.Errorf("failed to populate provider access config: %w", err)
}

jumpServers := make([]ssh.ServerConfig, len(credentials.JumpServers))
for i, jumpServer := range credentials.JumpServers {
jumpServers[i] = ssh.ServerConfig{
SshHost: jumpServer.Host,
SshPort: jumpServer.Port,
SshAuthMethod: jumpServer.AuthMethod,
SshUsername: jumpServer.Username,
SshPassword: jumpServer.Password,
SshKey: jumpServer.Key,
SshKeyPassphrase: jumpServer.KeyPassphrase,
jumpServers := make([]ssh.ServerConfig, len(credentials.JumpServers))
for i, jumpServer := range credentials.JumpServers {
jumpServers[i] = ssh.ServerConfig{
SshHost: jumpServer.Host,
SshPort: jumpServer.Port,
SshAuthMethod: jumpServer.AuthMethod,
SshUsername: jumpServer.Username,
SshPassword: jumpServer.Password,
SshKey: jumpServer.Key,
SshKeyPassphrase: jumpServer.KeyPassphrase,
}
}
}

provider, err := ssh.NewChallengeProvider(&ssh.ChallengeProviderConfig{
ServerConfig: ssh.ServerConfig{
SshHost: credentials.Host,
SshPort: credentials.Port,
SshAuthMethod: credentials.AuthMethod,
SshUsername: credentials.Username,
SshPassword: credentials.Password,
SshKey: credentials.Key,
SshKeyPassphrase: credentials.KeyPassphrase,
},
JumpServers: jumpServers,
UseSCP: xmaps.GetBool(options.ProviderExtendedConfig, "useSCP"),
WebRootPath: xmaps.GetString(options.ProviderExtendedConfig, "webRootPath"),
})
return provider, err
}); err != nil {
panic(err)
provider, err := ssh.NewChallengeProvider(&ssh.ChallengeProviderConfig{
ServerConfig: ssh.ServerConfig{
SshHost: credentials.Host,
SshPort: credentials.Port,
SshAuthMethod: credentials.AuthMethod,
SshUsername: credentials.Username,
SshPassword: credentials.Password,
SshKey: credentials.Key,
SshKeyPassphrase: credentials.KeyPassphrase,
},
JumpServers: jumpServers,
UseSCP: xmaps.GetBool(options.ProviderExtendedConfig, "useSCP"),
WebRootPath: xmaps.GetString(options.ProviderExtendedConfig, "webRootPath"),
})
return provider, err
}); err != nil {
panic(err)
}
}

register(domain.ACMEHttp01ProviderTypeSSH)
register(domain.ACMEHttp01ProviderTypeDockerHost)
}
91 changes: 48 additions & 43 deletions internal/certdeploy/deployers/sp_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,56 @@ import (
)

func init() {
if err := Registries.Register(domain.DeploymentProviderTypeSSH, func(options *ProviderFactoryOptions) (core.SSLDeployer, error) {
credentials := domain.AccessConfigForSSH{}
if err := xmaps.Populate(options.ProviderAccessConfig, &credentials); err != nil {
return nil, fmt.Errorf("failed to populate provider access config: %w", err)
}
register := func(providerType domain.DeploymentProviderType) {
if err := Registries.Register(providerType, func(options *ProviderFactoryOptions) (core.SSLDeployer, error) {
credentials := domain.AccessConfigForSSH{}
if err := xmaps.Populate(options.ProviderAccessConfig, &credentials); err != nil {
return nil, fmt.Errorf("failed to populate provider access config: %w", err)
}

jumpServers := make([]ssh.ServerConfig, len(credentials.JumpServers))
for i, jumpServer := range credentials.JumpServers {
jumpServers[i] = ssh.ServerConfig{
SshHost: jumpServer.Host,
SshPort: jumpServer.Port,
SshAuthMethod: jumpServer.AuthMethod,
SshUsername: jumpServer.Username,
SshPassword: jumpServer.Password,
SshKey: jumpServer.Key,
SshKeyPassphrase: jumpServer.KeyPassphrase,
jumpServers := make([]ssh.ServerConfig, len(credentials.JumpServers))
for i, jumpServer := range credentials.JumpServers {
jumpServers[i] = ssh.ServerConfig{
SshHost: jumpServer.Host,
SshPort: jumpServer.Port,
SshAuthMethod: jumpServer.AuthMethod,
SshUsername: jumpServer.Username,
SshPassword: jumpServer.Password,
SshKey: jumpServer.Key,
SshKeyPassphrase: jumpServer.KeyPassphrase,
}
}
}

provider, err := ssh.NewSSLDeployerProvider(&ssh.SSLDeployerProviderConfig{
ServerConfig: ssh.ServerConfig{
SshHost: credentials.Host,
SshPort: credentials.Port,
SshAuthMethod: credentials.AuthMethod,
SshUsername: credentials.Username,
SshPassword: credentials.Password,
SshKey: credentials.Key,
SshKeyPassphrase: credentials.KeyPassphrase,
},
JumpServers: jumpServers,
UseSCP: xmaps.GetBool(options.ProviderExtendedConfig, "useSCP"),
PreCommand: xmaps.GetString(options.ProviderExtendedConfig, "preCommand"),
PostCommand: xmaps.GetString(options.ProviderExtendedConfig, "postCommand"),
OutputFormat: ssh.OutputFormatType(xmaps.GetOrDefaultString(options.ProviderExtendedConfig, "format", string(ssh.OUTPUT_FORMAT_PEM))),
OutputKeyPath: xmaps.GetString(options.ProviderExtendedConfig, "keyPath"),
OutputCertPath: xmaps.GetString(options.ProviderExtendedConfig, "certPath"),
OutputServerCertPath: xmaps.GetString(options.ProviderExtendedConfig, "certPathForServerOnly"),
OutputIntermediaCertPath: xmaps.GetString(options.ProviderExtendedConfig, "certPathForIntermediaOnly"),
PfxPassword: xmaps.GetString(options.ProviderExtendedConfig, "pfxPassword"),
JksAlias: xmaps.GetString(options.ProviderExtendedConfig, "jksAlias"),
JksKeypass: xmaps.GetString(options.ProviderExtendedConfig, "jksKeypass"),
JksStorepass: xmaps.GetString(options.ProviderExtendedConfig, "jksStorepass"),
})
return provider, err
}); err != nil {
panic(err)
provider, err := ssh.NewSSLDeployerProvider(&ssh.SSLDeployerProviderConfig{
ServerConfig: ssh.ServerConfig{
SshHost: credentials.Host,
SshPort: credentials.Port,
SshAuthMethod: credentials.AuthMethod,
SshUsername: credentials.Username,
SshPassword: credentials.Password,
SshKey: credentials.Key,
SshKeyPassphrase: credentials.KeyPassphrase,
},
JumpServers: jumpServers,
UseSCP: xmaps.GetBool(options.ProviderExtendedConfig, "useSCP"),
PreCommand: xmaps.GetString(options.ProviderExtendedConfig, "preCommand"),
PostCommand: xmaps.GetString(options.ProviderExtendedConfig, "postCommand"),
OutputFormat: ssh.OutputFormatType(xmaps.GetOrDefaultString(options.ProviderExtendedConfig, "format", string(ssh.OUTPUT_FORMAT_PEM))),
OutputKeyPath: xmaps.GetString(options.ProviderExtendedConfig, "keyPath"),
OutputCertPath: xmaps.GetString(options.ProviderExtendedConfig, "certPath"),
OutputServerCertPath: xmaps.GetString(options.ProviderExtendedConfig, "certPathForServerOnly"),
OutputIntermediaCertPath: xmaps.GetString(options.ProviderExtendedConfig, "certPathForIntermediaOnly"),
PfxPassword: xmaps.GetString(options.ProviderExtendedConfig, "pfxPassword"),
JksAlias: xmaps.GetString(options.ProviderExtendedConfig, "jksAlias"),
JksKeypass: xmaps.GetString(options.ProviderExtendedConfig, "jksKeypass"),
JksStorepass: xmaps.GetString(options.ProviderExtendedConfig, "jksStorepass"),
})
return provider, err
}); err != nil {
panic(err)
}
}

register(domain.DeploymentProviderTypeSSH)
register(domain.DeploymentProviderTypeDockerHost)
}
7 changes: 5 additions & 2 deletions internal/domain/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
AccessProviderTypeDigitalOcean = AccessProviderType("digitalocean")
AccessProviderTypeDingTalkBot = AccessProviderType("dingtalkbot")
AccessProviderTypeDiscordBot = AccessProviderType("discordbot")
AccessProviderTypeDockerHost = AccessProviderType("dockerhost")
AccessProviderTypeDNSLA = AccessProviderType("dnsla")
AccessProviderTypeDogeCloud = AccessProviderType("dogecloud")
AccessProviderTypeDuckDNS = AccessProviderType("duckdns")
Expand Down Expand Up @@ -205,8 +206,9 @@ ACME HTTP-01 提供商常量值。
NOTICE: If you add new constant, please keep ASCII order.
*/
const (
ACMEHttp01ProviderTypeLocal = ACMEHttp01ProviderType(AccessProviderTypeLocal)
ACMEHttp01ProviderTypeSSH = ACMEHttp01ProviderType(AccessProviderTypeSSH)
ACMEHttp01ProviderTypeLocal = ACMEHttp01ProviderType(AccessProviderTypeLocal)
ACMEHttp01ProviderTypeSSH = ACMEHttp01ProviderType(AccessProviderTypeSSH)
ACMEHttp01ProviderTypeDockerHost = ACMEHttp01ProviderType(AccessProviderTypeDockerHost)
)

type DeploymentProviderType string
Expand Down Expand Up @@ -290,6 +292,7 @@ const (
DeploymentProviderTypeRatPanelSite = DeploymentProviderType(AccessProviderTypeRatPanel + "-site")
DeploymentProviderTypeSafeLine = DeploymentProviderType(AccessProviderTypeSafeLine)
DeploymentProviderTypeSSH = DeploymentProviderType(AccessProviderTypeSSH)
DeploymentProviderTypeDockerHost = DeploymentProviderType(AccessProviderTypeDockerHost)
DeploymentProviderTypeTencentCloudCDN = DeploymentProviderType(AccessProviderTypeTencentCloud + "-cdn")
DeploymentProviderTypeTencentCloudCLB = DeploymentProviderType(AccessProviderTypeTencentCloud + "-clb")
DeploymentProviderTypeTencentCloudCOS = DeploymentProviderType(AccessProviderTypeTencentCloud + "-cos")
Expand Down
35 changes: 35 additions & 0 deletions internal/rest/handlers/system.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package handlers

import (
"context"

"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/router"

"github.com/certimate-go/certimate/internal/rest/resp"
"github.com/certimate-go/certimate/internal/system"
)

type environmentService interface {
GetEnvironment(context.Context) (*system.Environment, error)
}

type SystemHandler struct {
service environmentService
}

func NewSystemHandler(router *router.RouterGroup[*core.RequestEvent], service environmentService) {
handler := &SystemHandler{service: service}

group := router.Group("/system")
group.GET("/environment", handler.getEnvironment)
}

func (handler *SystemHandler) getEnvironment(e *core.RequestEvent) error {
env, err := handler.service.GetEnvironment(e.Request.Context())
if err != nil {
return resp.Err(e, err)
}

return resp.Ok(e, env)
}
4 changes: 4 additions & 0 deletions internal/rest/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/certimate-go/certimate/internal/repository"
"github.com/certimate-go/certimate/internal/rest/handlers"
"github.com/certimate-go/certimate/internal/statistics"
"github.com/certimate-go/certimate/internal/system"
"github.com/certimate-go/certimate/internal/workflow"
)

Expand All @@ -20,6 +21,7 @@ var (
workflowSvc *workflow.WorkflowService
statisticsSvc *statistics.StatisticsService
notifySvc *notify.NotifyService
systemSvc *system.EnvironmentService
)

func Register(router *router.Router[*core.RequestEvent]) {
Expand All @@ -34,12 +36,14 @@ func Register(router *router.Router[*core.RequestEvent]) {
workflowSvc = workflow.NewWorkflowService(workflowRepo, workflowRunRepo, settingsRepo)
statisticsSvc = statistics.NewStatisticsService(statisticsRepo)
notifySvc = notify.NewNotifyService(accessRepo)
systemSvc = system.NewEnvironmentService(nil)

group := router.Group("/api")
group.Bind(apis.RequireSuperuserAuth())
handlers.NewCertificateHandler(group, certificateSvc)
handlers.NewWorkflowHandler(group, workflowSvc)
handlers.NewStatisticsHandler(group, statisticsSvc)
handlers.NewSystemHandler(group, systemSvc)
handlers.NewNotifyHandler(group, notifySvc)
}

Expand Down
35 changes: 35 additions & 0 deletions internal/system/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package system

import (
"context"

"github.com/certimate-go/certimate/pkg/utils/netutil"
)

type DockerHostInfo struct {
Reachable bool `json:"reachable"`
Address string `json:"address,omitempty"`
}

type Environment struct {
DockerHost DockerHostInfo `json:"dockerHost"`
}

type EnvironmentService struct {
resolver netutil.IPResolver
}

func NewEnvironmentService(resolver netutil.IPResolver) *EnvironmentService {
return &EnvironmentService{resolver: resolver}
}

func (s *EnvironmentService) GetEnvironment(ctx context.Context) (*Environment, error) {
addr, ok := netutil.LookupDockerHost(ctx, s.resolver)

return &Environment{
DockerHost: DockerHostInfo{
Reachable: ok,
Address: addr,
},
}, nil
}
50 changes: 50 additions & 0 deletions internal/system/environment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package system

import (
"context"
"net"
"testing"
)

type stubResolver struct {
addrs []net.IPAddr
err error
}

func (s stubResolver) LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error) {
return s.addrs, s.err
}

func TestEnvironmentService_GetEnvironment(t *testing.T) {
resolver := stubResolver{addrs: []net.IPAddr{{IP: net.ParseIP("172.17.0.1")}}}
svc := NewEnvironmentService(resolver)

env, err := svc.GetEnvironment(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if env.DockerHost.Address != "172.17.0.1" {
t.Fatalf("expected docker host address '172.17.0.1', got %q", env.DockerHost.Address)
}
if !env.DockerHost.Reachable {
t.Fatalf("expected docker host to be reachable")
}
}

func TestEnvironmentService_GetEnvironment_Unreachable(t *testing.T) {
resolver := stubResolver{}
svc := NewEnvironmentService(resolver)

env, err := svc.GetEnvironment(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if env.DockerHost.Address != "" {
t.Fatalf("expected empty address, got %q", env.DockerHost.Address)
}
if env.DockerHost.Reachable {
t.Fatalf("expected docker host to be unreachable")
}
}
Loading