pylego 0.1.2__py3-none-any.whl → 0.1.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pylego/lego.go ADDED
@@ -0,0 +1,179 @@
1
+ package main
2
+
3
+ import "C"
4
+ import (
5
+ "crypto"
6
+ "crypto/ecdsa"
7
+ "crypto/elliptic"
8
+ "crypto/rand"
9
+ "crypto/x509"
10
+ "encoding/json"
11
+ "encoding/pem"
12
+ "errors"
13
+ "fmt"
14
+ "os"
15
+
16
+ "github.com/go-acme/lego/v4/certcrypto"
17
+ "github.com/go-acme/lego/v4/certificate"
18
+ "github.com/go-acme/lego/v4/challenge/http01"
19
+ "github.com/go-acme/lego/v4/challenge/tlsalpn01"
20
+ "github.com/go-acme/lego/v4/lego"
21
+ "github.com/go-acme/lego/v4/providers/dns"
22
+ "github.com/go-acme/lego/v4/registration"
23
+ )
24
+
25
+ type LegoInputArgs struct {
26
+ Email string `json:"email"`
27
+ Server string `json:"server"`
28
+ CSR string `json:"csr"`
29
+ Plugin string `json:"plugin"`
30
+ Env map[string]string
31
+ }
32
+
33
+ type LegoOutputResponse struct {
34
+ CSR string `json:"csr"`
35
+ PrivateKey string `json:"private_key"`
36
+ Certificate string `json:"certificate"`
37
+ IssuerCertificate string `json:"issuer_certificate"`
38
+ Metadata `json:"metadata"`
39
+ }
40
+
41
+ type Metadata struct {
42
+ StableURL string `json:"stable_url"`
43
+ URL string `json:"url"`
44
+ Domain string `json:"domain"`
45
+ }
46
+
47
+ //export RunLegoCommand
48
+ func RunLegoCommand(message *C.char) *C.char {
49
+ CLIArgs, err := extractArguments(C.GoString(message))
50
+ if err != nil {
51
+ return C.CString(fmt.Sprint("error: couldn't extract arguments: ", err))
52
+ }
53
+ for k, v := range CLIArgs.Env {
54
+ if err := os.Setenv(k, v); err != nil {
55
+ return C.CString(fmt.Sprint("error: couldn't load environment variables: ", err))
56
+ }
57
+
58
+ }
59
+ certificate, err := requestCertificate(CLIArgs.Email, CLIArgs.Server, CLIArgs.CSR, CLIArgs.Plugin)
60
+ if err != nil {
61
+ return C.CString(fmt.Sprint("error: couldn't request certificate: ", err))
62
+ }
63
+ response_json, err := json.Marshal(certificate)
64
+ if err != nil {
65
+ return C.CString(fmt.Sprint("error: coudn't build response message: ", err))
66
+ }
67
+ return_message_ptr := C.CString(string(response_json))
68
+ return return_message_ptr
69
+ }
70
+
71
+ func requestCertificate(email, server, csr, plugin string) (*LegoOutputResponse, error) {
72
+ privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
73
+ if err != nil {
74
+ return nil, fmt.Errorf("couldn't generate priv key: %s", err)
75
+ }
76
+ user := LetsEncryptUser{
77
+ Email: email,
78
+ key: privateKey,
79
+ }
80
+ config := lego.NewConfig(&user)
81
+
82
+ config.CADirURL = server
83
+ config.Certificate.KeyType = certcrypto.RSA2048
84
+
85
+ client, err := lego.NewClient(config)
86
+ if err != nil {
87
+ return nil, fmt.Errorf("couldn't create lego client: %s", err)
88
+ }
89
+
90
+ err = configureClientChallenges(client, plugin)
91
+ if err != nil {
92
+ return nil, fmt.Errorf("couldn't configure client challenges: %s", err)
93
+ }
94
+
95
+ reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
96
+ if err != nil {
97
+ return nil, fmt.Errorf("couldn't register user: %s", err)
98
+ }
99
+ user.Registration = reg
100
+
101
+ block, _ := pem.Decode([]byte(csr))
102
+ if block == nil || block.Type != "CERTIFICATE REQUEST" {
103
+ return nil, errors.New("failed to decode PEM block containing certificate request")
104
+ }
105
+ csrObject, err := x509.ParseCertificateRequest(block.Bytes)
106
+ if err != nil {
107
+ return nil, fmt.Errorf("failed to parse certificate request: %s", err)
108
+ }
109
+ request := certificate.ObtainForCSRRequest{
110
+ CSR: csrObject,
111
+ Bundle: true,
112
+ }
113
+ certificates, err := client.Certificate.ObtainForCSR(request)
114
+ if err != nil {
115
+ return nil, fmt.Errorf("coudn't obtain cert: %s", err)
116
+ }
117
+
118
+ return &LegoOutputResponse{
119
+ CSR: string(certificates.CSR),
120
+ PrivateKey: string(certificates.PrivateKey),
121
+ Certificate: string(certificates.Certificate),
122
+ IssuerCertificate: string(certificates.IssuerCertificate),
123
+ Metadata: Metadata{
124
+ StableURL: certificates.CertStableURL,
125
+ URL: certificates.CertURL,
126
+ Domain: certificates.Domain,
127
+ },
128
+ }, nil
129
+ }
130
+
131
+ func configureClientChallenges(client *lego.Client, plugin string) error {
132
+ switch plugin {
133
+ case "":
134
+ err := client.Challenge.SetHTTP01Provider(http01.NewProviderServer(os.Getenv("HTTP01_IFACE"), os.Getenv("HTTP01_PORT")))
135
+ if err != nil {
136
+ return errors.Join(errors.New("couldn't set http01 provider server: "), err)
137
+ }
138
+ err = client.Challenge.SetTLSALPN01Provider(tlsalpn01.NewProviderServer(os.Getenv("TLSALPN01_IFACE"), os.Getenv("TLSALPN01_PORT")))
139
+ if err != nil {
140
+ return errors.Join(errors.New("couldn't set tlsalpn01 provider server: "), err)
141
+ }
142
+ default:
143
+ dnsProvider, err := dns.NewDNSChallengeProviderByName(plugin)
144
+ if err != nil {
145
+ return errors.Join(fmt.Errorf("couldn't create %s provider: ", plugin), err)
146
+ }
147
+ err = client.Challenge.SetDNS01Provider(dnsProvider)
148
+ if err != nil {
149
+ return errors.Join(fmt.Errorf("couldn't set %s DNS provider server: ", plugin), err)
150
+ }
151
+ }
152
+ return nil
153
+ }
154
+
155
+ type LetsEncryptUser struct {
156
+ Email string
157
+ Registration *registration.Resource
158
+ key crypto.PrivateKey
159
+ }
160
+
161
+ func (u *LetsEncryptUser) GetEmail() string {
162
+ return u.Email
163
+ }
164
+ func (u LetsEncryptUser) GetRegistration() *registration.Resource {
165
+ return u.Registration
166
+ }
167
+ func (u *LetsEncryptUser) GetPrivateKey() crypto.PrivateKey {
168
+ return u.key
169
+ }
170
+
171
+ func extractArguments(jsonMessage string) (LegoInputArgs, error) {
172
+ var CLIArgs LegoInputArgs
173
+ if err := json.Unmarshal([]byte(jsonMessage), &CLIArgs); err != nil {
174
+ return CLIArgs, errors.Join(errors.New("cli args failed validation: "), err)
175
+ }
176
+ return CLIArgs, nil
177
+ }
178
+
179
+ func main() {}
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pylego
3
- Version: 0.1.2
3
+ Version: 0.1.5
4
4
  Summary: A python wrapper package for the lego application written in Golang
5
5
  Author-email: Canonical <telco-engineers@lists.canonical.com>
6
6
  Project-URL: Homepage, https://github.com/canonical/pylego
@@ -12,8 +12,9 @@ Requires-Python: >=3.8
12
12
  Description-Content-Type: text/markdown
13
13
  License-File: LICENSE
14
14
  Provides-Extra: test
15
- Requires-Dist: pytest ; extra == 'test'
16
- Requires-Dist: ruff ; extra == 'test'
15
+ Requires-Dist: pytest; extra == "test"
16
+ Requires-Dist: requests; extra == "test"
17
+ Requires-Dist: ruff; extra == "test"
17
18
 
18
19
  # pylego
19
20
 
@@ -0,0 +1,11 @@
1
+ pylego/__init__.py,sha256=7rcUcQcOWsOLxTOEXF2ASkwm_7eED1UIXzxdlgKPr5c,82
2
+ pylego/go.mod,sha256=8WzL-rtbt5onJbzLCU6XAXfDAd_nhPFPg5D-gSyZj94,9471
3
+ pylego/go.sum,sha256=h0v7-uaOYJBhVAOI8TImmCektYxMkf4mXQk2XZPgXrI,97959
4
+ pylego/lego.go,sha256=52e3iR6uc70EszJDNYCpM_78Cb6zZuYj1kK9L5ZBz9Y,5281
5
+ pylego/lego.so,sha256=rpi2KEj_rsKAXTjUnWFS2YHRxtKM5OUlkFMBcilMo1A,139818208
6
+ pylego/pylego.py,sha256=bHsVzqMM2DZmhCAn-yqlxKQ4Pr5QRyOpBjP2dJSBKMU,1976
7
+ pylego-0.1.5.dist-info/LICENSE,sha256=aklz9Y8CIpFsN61U4jHlJYp4W_8HoDpY-tINlDcdSZY,10934
8
+ pylego-0.1.5.dist-info/METADATA,sha256=TztXvsE2sSN4Q5Yfp3LRVB50YQgJ8BVp7ELcG9a4vyU,5528
9
+ pylego-0.1.5.dist-info/WHEEL,sha256=HiCZjzuy6Dw0hdX5R3LCFPDmFS4BWl8H-8W39XfmgX4,91
10
+ pylego-0.1.5.dist-info/top_level.txt,sha256=pSOYv55_w90qy3xOvqz_ysSz-X-XRTb-jMpiOyLNnNs,7
11
+ pylego-0.1.5.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (72.0.0)
2
+ Generator: setuptools (72.2.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,8 +0,0 @@
1
- pylego/__init__.py,sha256=sYJobFIJ2iCzyicqTfQmqggMC-6-t9eO8fCtxG_6Q60,57
2
- pylego/lego.so,sha256=rpi2KEj_rsKAXTjUnWFS2YHRxtKM5OUlkFMBcilMo1A,139818208
3
- pylego/pylego.py,sha256=bHsVzqMM2DZmhCAn-yqlxKQ4Pr5QRyOpBjP2dJSBKMU,1976
4
- pylego-0.1.2.dist-info/LICENSE,sha256=aklz9Y8CIpFsN61U4jHlJYp4W_8HoDpY-tINlDcdSZY,10934
5
- pylego-0.1.2.dist-info/METADATA,sha256=Pv59fdUzVFSCycCXkV8dy5uk0FfOU8-n2C0u8B3lESE,5489
6
- pylego-0.1.2.dist-info/WHEEL,sha256=Rp8gFpivVLXx-k3U95ozHnQw8yDcPxmhOpn_Gx8d5nc,91
7
- pylego-0.1.2.dist-info/top_level.txt,sha256=pSOYv55_w90qy3xOvqz_ysSz-X-XRTb-jMpiOyLNnNs,7
8
- pylego-0.1.2.dist-info/RECORD,,