Skip to content

Commit f986e55

Browse files
committed
rebuild old code
1 parent 198e652 commit f986e55

File tree

7 files changed

+98
-0
lines changed

7 files changed

+98
-0
lines changed

Diff for: api/dataservices/interface.go

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type (
4949
User() UserService
5050
Version() VersionService
5151
Webhook() WebhookService
52+
Key() KeyService
5253
}
5354

5455
// CustomTemplateService represents a service to manage custom templates
@@ -320,6 +321,11 @@ type (
320321
DeleteWebhook(ID portainer.WebhookID) error
321322
BucketName() string
322323
}
324+
325+
KeyService interface {
326+
Keys() ([]portainer.Key, error)
327+
Create(keyObject *portainer.Key, data string)
328+
}
323329
)
324330

325331
func IsErrObjectNotFound(e error) bool {

Diff for: api/dataservices/key/key.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package key
2+
3+
import (
4+
portainer "github.com/portainer/portainer/api"
5+
)
6+
7+
const (
8+
BucketName = "keys"
9+
)
10+
11+
type Service struct {
12+
connection portainer.Connection
13+
}
14+
15+
func (service *Service) BucketName() string {
16+
return BucketName
17+
}
18+
19+
// NewService creates a new instance of this conf. compute service.
20+
func NewService(connection portainer.Connection) (*Service, error) {
21+
err := connection.SetServiceName(BucketName)
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
return &Service{
27+
connection: connection,
28+
}, nil
29+
}
30+
31+
func (service *Service) Create(keyObject *portainer.Key, data string) error {
32+
33+
//only generate if not set
34+
// if keyObject.Key == nil {
35+
// // generate new rsa key
36+
// privatekey, err := GenerateMultiPrimeKeyForSGX(rand.Reader, 2, 3072)
37+
// if err != nil {
38+
// fmt.Fprintf(os.Stderr, "Cannot generate RSA key\n")
39+
// return errors.New("Could not generate Key")
40+
// }
41+
42+
// keyObject.Key = privatekey
43+
// }
44+
45+
return service.connection.CreateObject(
46+
BucketName,
47+
func(id uint64) (int, interface{}) {
48+
keyObject.ID = portainer.KeyID(id)
49+
return int(id), keyObject
50+
},
51+
)
52+
}

Diff for: api/datastore/migrator/migrator.go

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/portainer/portainer/api/dataservices/endpointrelation"
99
"github.com/portainer/portainer/api/dataservices/extension"
1010
"github.com/portainer/portainer/api/dataservices/fdoprofile"
11+
"github.com/portainer/portainer/api/dataservices/key"
1112
"github.com/portainer/portainer/api/dataservices/registry"
1213
"github.com/portainer/portainer/api/dataservices/resourcecontrol"
1314
"github.com/portainer/portainer/api/dataservices/role"
@@ -45,6 +46,7 @@ type (
4546
fileService portainer.FileService
4647
authorizationService *authorization.Service
4748
dockerhubService *dockerhub.Service
49+
keyService *key.Service
4850
}
4951

5052
// MigratorParameters represents the required parameters to create a new Migrator instance.
@@ -69,6 +71,7 @@ type (
6971
FileService portainer.FileService
7072
AuthorizationService *authorization.Service
7173
DockerhubService *dockerhub.Service
74+
KeyService *key.Service
7275
}
7376
)
7477

@@ -95,6 +98,7 @@ func NewMigrator(parameters *MigratorParameters) *Migrator {
9598
fileService: parameters.FileService,
9699
authorizationService: parameters.AuthorizationService,
97100
dockerhubService: parameters.DockerhubService,
101+
keyService: parameters.KeyService,
98102
}
99103
}
100104

Diff for: api/datastore/services.go

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/portainer/portainer/api/dataservices/extension"
2222
"github.com/portainer/portainer/api/dataservices/fdoprofile"
2323
"github.com/portainer/portainer/api/dataservices/helmuserrepository"
24+
"github.com/portainer/portainer/api/dataservices/key"
2425
"github.com/portainer/portainer/api/dataservices/registry"
2526
"github.com/portainer/portainer/api/dataservices/resourcecontrol"
2627
"github.com/portainer/portainer/api/dataservices/role"
@@ -74,6 +75,7 @@ type Store struct {
7475
UserService *user.Service
7576
VersionService *version.Service
7677
WebhookService *webhook.Service
78+
KeyService *key.Service
7779
}
7880

7981
func (store *Store) initServices() error {
@@ -245,6 +247,12 @@ func (store *Store) initServices() error {
245247
}
246248
store.ScheduleService = scheduleService
247249

250+
keyService, err := key.NewService(store.connection)
251+
if err != nil {
252+
return err
253+
}
254+
store.KeyService = keyService
255+
248256
return nil
249257
}
250258

Diff for: api/http/handler/portainercc/handler.go

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func NewHandler(bouncer *security.RequestBouncer, dataStore dataservices.DataSto
2626
restrictedRouter := h.NewRoute().Subrouter()
2727
restrictedRouter.Use(bouncer.RestrictedAccess)
2828

29+
restrictedRouter.Handle("/portainercc/keys", httperror.LoggerHandler(h.generateOrImport)).Methods(http.MethodGet)
2930
restrictedRouter.Handle("/portainercc/keys", httperror.LoggerHandler(h.generateOrImport)).Methods(http.MethodPost)
3031

3132
return h

Diff for: api/http/handler/portainercc/key.go

+26
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package portainercc
22

33
import (
44
"encoding/json"
5+
"log"
56
"net/http"
67

78
httperror "github.com/portainer/libhttp/error"
9+
"github.com/portainer/libhttp/response"
810
portainer "github.com/portainer/portainer/api"
911
)
1012

@@ -32,5 +34,29 @@ func (handler *Handler) generateOrImport(w http.ResponseWriter, r *http.Request)
3234
return httperror.BadRequest("request body malefomred", err)
3335
}
3436

37+
keyObject := &portainer.Key{
38+
KeyType: params.KeyType,
39+
Description: params.Description,
40+
TeamAccessPolicies: params.TeamAccessPolicies,
41+
}
42+
43+
handler.DataStore.Key().Create(keyObject, params.Data)
44+
45+
log.Print("AHA?")
46+
log.Print(keyObject)
47+
log.Printf("BODY: %s", params.Data)
48+
49+
log.Print(params.Data != "")
50+
3551
return httperror.BadRequest("invalid body content", nil)
3652
}
53+
54+
func (handler *Handler) getKeys(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
55+
keys, err := handler.DataStore.Key().Keys()
56+
57+
if err != nil {
58+
return httperror.InternalServerError("couldn retrive keys from db", err)
59+
}
60+
61+
return response.JSON(w, keys)
62+
}

Diff for: example_pf.key

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
KB�,�\�8��d�!l�

0 commit comments

Comments
 (0)