Skip to content
Snippets Groups Projects
Commit 908a6318 authored by Nguyen Minh Dang's avatar Nguyen Minh Dang
Browse files

WIP

parent 058a322c
No related branches found
No related tags found
No related merge requests found
Pipeline #39436 failed
...@@ -3,9 +3,11 @@ package api ...@@ -3,9 +3,11 @@ package api
import ( import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net"
"net/http" "net/http"
"strconv" "strconv"
"sync" "sync"
"time"
) )
var server *gin.Engine var server *gin.Engine
...@@ -30,6 +32,7 @@ func errorHandler() gin.HandlerFunc { ...@@ -30,6 +32,7 @@ func errorHandler() gin.HandlerFunc {
func defineEndpoints() { func defineEndpoints() {
server.POST("/v1/:tenantId/revoke/:blockId/:index", handleRevoke) server.POST("/v1/:tenantId/revoke/:blockId/:index", handleRevoke)
server.GET("/v1/:tenantId/statuslist/:blockid", handleStatuslist)
} }
func handleRevoke(ctx *gin.Context) { func handleRevoke(ctx *gin.Context) {
...@@ -59,6 +62,22 @@ func handleRevoke(ctx *gin.Context) { ...@@ -59,6 +62,22 @@ func handleRevoke(ctx *gin.Context) {
}) })
} }
func handleStatuslist(ctx *gin.Context) {
reqTime := time.Now()
exp := reqTime.Add(config.CurrentRevocationConfig.Expiry).Unix()
iat := reqTime.Unix()
iss, _, _ := net.SplitHostPort(ctx.Request.Host)
sub := ctx.Request.URL.String()
tenantId := ctx.Param("tenantId")
blockId, err := strconv.Atoi(ctx.Param("blockId"))
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
}
func startRest(port int, wg *sync.WaitGroup) { func startRest(port int, wg *sync.WaitGroup) {
defer wg.Done() defer wg.Done()
......
...@@ -3,13 +3,15 @@ package config ...@@ -3,13 +3,15 @@ package config
import ( import (
"gitlab.eclipse.org/eclipse/xfsc/libraries/microservice/core/pkg/config" "gitlab.eclipse.org/eclipse/xfsc/libraries/microservice/core/pkg/config"
pgPkg "gitlab.eclipse.org/eclipse/xfsc/libraries/microservice/core/pkg/db/postgres" pgPkg "gitlab.eclipse.org/eclipse/xfsc/libraries/microservice/core/pkg/db/postgres"
"time"
) )
type statusListConfiguration struct { type statusListConfiguration struct {
config.BaseConfig `mapstructure:",squash"` config.BaseConfig `mapstructure:",squash"`
Database pgPkg.Config `mapstructure:"database"` Database pgPkg.Config `mapstructure:"database"`
CreationTopic string `mapstructure:"creationTopic"` CreationTopic string `mapstructure:"creationTopic"`
BlockSizeInBytes int `mapstructure:"blockSizeInBytes"` BlockSizeInBytes int `mapstructure:"blockSizeInBytes"`
Expiry time.Duration `mapstructure:"expiry"`
} }
var CurrentStatusListConfig statusListConfiguration var CurrentStatusListConfig statusListConfiguration
...@@ -22,5 +24,6 @@ func getDefaults() map[string]any { ...@@ -22,5 +24,6 @@ func getDefaults() map[string]any {
return map[string]any{ return map[string]any{
"isDev": false, "isDev": false,
"creationTopic": "status", "creationTopic": "status",
"expiry": "5m",
} }
} }
...@@ -220,6 +220,10 @@ func (pc *postgresConnection) CreateTableForTenantIdIfNotExists(ctx context.Cont ...@@ -220,6 +220,10 @@ func (pc *postgresConnection) CreateTableForTenantIdIfNotExists(ctx context.Cont
return nil return nil
} }
func (pc *postgresConnection) GetBlockForTenantId(ctx context.Context, tenantId string, blockId int) error {
}
func (pc *postgresConnection) Close() { func (pc *postgresConnection) Close() {
pc.conn.Close() pc.conn.Close()
} }
......
package entity package entity
import "fmt" import (
"bytes"
"compress/gzip"
"encoding/base64"
"fmt"
)
var ErrFullyAllocated = fmt.Errorf("block is already fully allocated") var ErrFullyAllocated = fmt.Errorf("block is already fully allocated")
...@@ -40,3 +45,20 @@ func (b *Block) AllocateNextFreeIndex() (index int, err error) { ...@@ -40,3 +45,20 @@ func (b *Block) AllocateNextFreeIndex() (index int, err error) {
return index, err return index, err
} }
func (b *Block) Encode() (string, error) {
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
if _, err := gz.Write(b.Block); err != nil {
err = fmt.Errorf("error occurred while compressing data with gzip: %w", err)
return "", err
}
if err := gz.Close(); err != nil {
err = fmt.Errorf("error occurred while closing gzip writer: %w", err)
return "", err
}
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
}
package entity package entity
import ( import (
"bytes"
"compress/gzip"
"encoding/base64"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"io"
"testing" "testing"
) )
...@@ -71,3 +75,34 @@ func TestTryToAllocateNewIndexInFullyAllocatedBlock(t *testing.T) { ...@@ -71,3 +75,34 @@ func TestTryToAllocateNewIndexInFullyAllocatedBlock(t *testing.T) {
_, err := block.AllocateNextFreeIndex() _, err := block.AllocateNextFreeIndex()
require.Error(t, ErrFullyAllocated, err) require.Error(t, ErrFullyAllocated, err)
} }
func TestEncodeBlock(t *testing.T) {
byteBlockSize := 1
block := NewBlock(byteBlockSize)
block.RevokeAtIndex(7)
block.RevokeAtIndex(5)
block.RevokeAtIndex(3)
s, _ := block.Encode()
require.Equal(t, "H4sIAAAAAAAA/3oFCAAA///r5N2SAQAAAA==", s)
decoded := decode(s)
require.Equal(t, decoded, block.Block)
block.RevokeAtIndex(6)
s, _ = block.Encode()
require.NotEqual(t, "H4sIAAAAAAAA/3oFCAAA///r5N2SAQAAAA==", s)
require.NotEqual(t, decoded, block.Block)
}
func decode(encode string) []byte {
decoded, _ := base64.StdEncoding.DecodeString(encode)
gr, _ := gzip.NewReader(bytes.NewBuffer(decoded))
defer gr.Close()
decompressedData, _ := io.ReadAll(gr)
return decompressedData
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment