mirror of
https://github.com/ultravioletrs/cocos.git
synced 2026-06-23 04:10:25 +00:00
5377dd4d7f
* Refactor mock interfaces to use 'any' instead of 'interface{}' for improved type safety and readability across multiple files in the manager and pkg directories.
Signed-off-by: Sammy Oina <sammyoina@gmail.com>
* Update Go version to 1.25.x in CI workflows and remove obsolete Go package files
Signed-off-by: Sammy Oina <sammyoina@gmail.com>
* Add mock implementations for various components in the attestation and SDK packages
- Created mock for MeasurementProvider in pkg/attestation/cmdconfig/mocks/mocks_test.go
- Created mock for Provider in pkg/attestation/mocks/mocks_test.go
- Created mock for Client in pkg/clients/grpc/mocks/mocks_test.go
- Created mock for SDK in pkg/sdk/mocks/mocks_test.go
These mocks are generated using mockery and are intended for unit testing purposes.
Signed-off-by: Sammy Oina <sammyoina@gmail.com>
* Remove autogenerated mock files and update mock usage in tests
- Deleted mocks for gRPC clients in pkg/clients/grpc/mocks/mocks_test.go and pkg/sdk/mocks/mocks_test.go.
- Updated test files in pkg/progressbar/progress_test.go to use the new mock structure without type parameters for gRPC client interfaces.
- Refactored mock generation in pkg/sdk/mocks/sdk.go to streamline the mock creation process and ensure consistency across mock methods.
Signed-off-by: Sammy Oina <sammyoina@gmail.com>
* Update protobuf generated files for events and manager
- Bump protoc-gen-go version from v1.36.5 to v1.36.8 in events.pb.go and manager.pb.go.
- Refactor raw descriptor definitions in events.pb.go and manager.pb.go to use string concatenation for better readability and maintainability.
- Ensure compatibility with the latest protobuf specifications and improve code generation consistency.
Signed-off-by: Sammy Oina <sammyoina@gmail.com>
* Update test commands to use GOTOOLCHAIN for consistent Go version handling
Signed-off-by: Sammy Oina <sammyoina@gmail.com>
* Fix GOTOOLCHAIN usage in test command for consistency
Signed-off-by: Sammy Oina <sammyoina@gmail.com>
---------
Signed-off-by: Sammy Oina <sammyoina@gmail.com>
174 lines
3.9 KiB
Go
174 lines
3.9 KiB
Go
// Copyright (c) Ultraviolet
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package auth
|
|
|
|
import (
|
|
"context"
|
|
"crypto"
|
|
"crypto/ecdsa"
|
|
"crypto/ed25519"
|
|
"crypto/rsa"
|
|
"crypto/sha256"
|
|
"crypto/x509"
|
|
"encoding/base64"
|
|
|
|
"github.com/absmach/supermq/pkg/errors"
|
|
"github.com/ultravioletrs/cocos/agent"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/metadata"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type UserRole string
|
|
|
|
const (
|
|
UserMetadataKey = "user-id"
|
|
SignatureMetadataKey = "signature"
|
|
ConsumerRole UserRole = "consumer"
|
|
DataProviderRole UserRole = "data-provider"
|
|
AlgorithmProviderRole UserRole = "algorithm-provider"
|
|
)
|
|
|
|
var (
|
|
ErrMissingMetadata = errors.New("missing metadata")
|
|
ErrInvalidMetadata = errors.New("invalid metadata")
|
|
ErrSignatureVerificationFailed = errors.New("signature verification failed")
|
|
)
|
|
|
|
type Authenticator interface {
|
|
AuthenticateUser(ctx context.Context, role UserRole) (context.Context, error)
|
|
}
|
|
|
|
type service struct {
|
|
resultConsumers []any
|
|
datasetProviders []any
|
|
algorithmProvider any
|
|
}
|
|
|
|
func New(manifest agent.Computation) (Authenticator, error) {
|
|
s := &service{}
|
|
for _, rc := range manifest.ResultConsumers {
|
|
pubKey, err := x509.ParsePKIXPublicKey(rc.UserKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pKey, err := decodePublicKey(pubKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
s.resultConsumers = append(s.resultConsumers, pKey)
|
|
}
|
|
|
|
for _, dp := range manifest.Datasets {
|
|
pubKey, err := x509.ParsePKIXPublicKey(dp.UserKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pKey, err := decodePublicKey(pubKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
s.datasetProviders = append(s.datasetProviders, pKey)
|
|
}
|
|
|
|
pubKey, err := x509.ParsePKIXPublicKey(manifest.Algorithm.UserKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pKey, err := decodePublicKey(pubKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
s.algorithmProvider = pKey
|
|
return s, nil
|
|
}
|
|
|
|
func extractSignature(md metadata.MD) (string, error) {
|
|
signature := md.Get(SignatureMetadataKey)
|
|
if len(signature) != 1 {
|
|
return "", status.Errorf(codes.Unauthenticated, "invalid metadata")
|
|
}
|
|
|
|
return signature[0], nil
|
|
}
|
|
|
|
func verifySignature(role UserRole, signature string, publicKey any) error {
|
|
hash := sha256.Sum256([]byte(role))
|
|
sigByte, err := base64.StdEncoding.DecodeString(signature)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var ok bool
|
|
|
|
switch publicKey := publicKey.(type) {
|
|
case *rsa.PublicKey:
|
|
if err = rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, hash[:], sigByte); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
case *ecdsa.PublicKey:
|
|
ok = ecdsa.VerifyASN1(publicKey, hash[:], sigByte)
|
|
case ed25519.PublicKey:
|
|
ok = ed25519.Verify(publicKey, []byte(role), sigByte)
|
|
}
|
|
|
|
if !ok {
|
|
return ErrSignatureVerificationFailed
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *service) AuthenticateUser(ctx context.Context, role UserRole) (context.Context, error) {
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
if !ok {
|
|
return nil, ErrMissingMetadata
|
|
}
|
|
signature, err := extractSignature(md)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, ErrInvalidMetadata)
|
|
}
|
|
|
|
switch role {
|
|
case ConsumerRole:
|
|
for i, rc := range s.resultConsumers {
|
|
if err := verifySignature(role, signature, rc); err == nil {
|
|
return agent.IndexToContext(ctx, i), nil
|
|
}
|
|
}
|
|
case DataProviderRole:
|
|
for _, dp := range s.datasetProviders {
|
|
if err := verifySignature(role, signature, dp); err == nil {
|
|
return ctx, nil
|
|
}
|
|
}
|
|
case AlgorithmProviderRole:
|
|
if err := verifySignature(role, signature, s.algorithmProvider); err == nil {
|
|
return ctx, nil
|
|
}
|
|
}
|
|
|
|
return ctx, ErrSignatureVerificationFailed
|
|
}
|
|
|
|
func decodePublicKey(key any) (pubKey any, err error) {
|
|
switch key := key.(type) {
|
|
case *rsa.PublicKey:
|
|
return key, nil
|
|
case *ecdsa.PublicKey:
|
|
return key, nil
|
|
case ed25519.PublicKey:
|
|
return key, nil
|
|
default:
|
|
return nil, errors.New("unsupported public key type")
|
|
}
|
|
}
|