Skip to content

Commit

Permalink
Merge 081dcc6 into 868f6f2
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph committed Mar 23, 2021
2 parents 868f6f2 + 081dcc6 commit 01712e7
Show file tree
Hide file tree
Showing 19 changed files with 251 additions and 205 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ test: test_goimports test_license test_vet test_golint test_staticcheck test_ine
test_goimports:
source .github/workflows/github.sh ; \
section "Test imports and code style" ; \
out=`$(GO_GOIMPORTS_BIN) -rel .` ; \
out=`$(GO_GOIMPORTS_BIN) -l .` ; \
echo "$${out}" ; \
test -z `echo $${out}`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,49 +28,33 @@ import (
"github.com/spf13/cobra"
)

var accountKeyAdvertiseCmd = &cobra.Command{
Use: "advertise",
Short: "Advertise new key to the key resolver",
var accountActivateCmd = &cobra.Command{
Use: "activate",
Short: "Activates an account on the key resolver",
Run: func(cmd *cobra.Command, args []string) {
v := vault.OpenDefaultVault()

info, err := vault.GetAccount(v, *akAccount)
info, err := vault.GetAccount(v, *aacAccount)
if err != nil {
fmt.Println("cannot find account in vault")
os.Exit(1)
}

vkp, err := info.FindKey(*akaKey)
if err != nil {
fmt.Println("cannot find key in account")
os.Exit(1)
}

info.SetActiveKey(&vkp.KeyPair)

// Save vault
err = v.Persist()
if err != nil {
fmt.Println("cannot save vault")
os.Exit(1)
}

ks := container.Instance.GetResolveService()
err = ks.UploadAddressInfo(*info, "")
err = ks.ActivateAccount(*info)
if err != nil {
fmt.Println("cannot advertise key")
fmt.Println("cannot activate account on the resolver")
os.Exit(1)
}

},
}

var akaKey *string
var aacAccount *string

func init() {
accountKeyCmd.AddCommand(accountKeyAdvertiseCmd)

akaKey = accountKeyAdvertiseCmd.Flags().StringP("key", "k", "", "fingerprint of the key to advertise")
accountCmd.AddCommand(accountActivateCmd)

_ = accountKeyAdvertiseCmd.MarkPersistentFlagRequired("account")
aacAccount = accountActivateCmd.Flags().String("account", "", "Account to create")
_ = accountActivateCmd.MarkPersistentFlagRequired("account")
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,44 @@
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package account
package cmd

import (
"encoding/json"
"fmt"
"os"

"github.com/bitmaelum/bitmaelum-suite/pkg/hash"
"github.com/bitmaelum/bitmaelum-suite/cmd/bm-client/internal/container"
"github.com/bitmaelum/bitmaelum-suite/internal/vault"
"github.com/spf13/cobra"
)

func (r *fileRepo) StoreOrganisationSettings(addr hash.Hash, settings OrganisationSettings) error {
data, err := json.MarshalIndent(settings, "", " ")
if err != nil {
return err
}
var accountDeactivateCmd = &cobra.Command{
Use: "deactivate",
Short: "Deactivates an account on the key resolver",
Run: func(cmd *cobra.Command, args []string) {
v := vault.OpenDefaultVault()

// And store
return r.store(addr, organisationFile, data)
info, err := vault.GetAccount(v, *adcAccount)
if err != nil {
fmt.Println("cannot find account in vault")
os.Exit(1)
}

ks := container.Instance.GetResolveService()
err = ks.DeactivateAccount(*info)
if err != nil {
fmt.Println("cannot deactivate account on the resolver")
os.Exit(1)
}

},
}

func (r *fileRepo) FetchOrganisationSettings(addr hash.Hash) (*OrganisationSettings, error) {
settings := &OrganisationSettings{}
err := r.fetchJSON(addr, keysFile, settings)
if err != nil {
return nil, err
}
var adcAccount *string

func init() {
accountCmd.AddCommand(accountDeactivateCmd)

return settings, nil
adcAccount = accountDeactivateCmd.Flags().String("account", "", "Account to create")
_ = accountDeactivateCmd.MarkPersistentFlagRequired("account")
}
2 changes: 0 additions & 2 deletions cmd/bm-client/cmd/organisation_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ var (
func init() {
organisationCmd.AddCommand(organisationValidateCmd)

accountCmd.AddCommand(accountKeyCmd)

ovOrganisation = organisationValidateCmd.PersistentFlags().StringP("organisation", "o", "", "Organisation")

_ = organisationValidateCmd.MarkPersistentFlagRequired("organisation")
Expand Down
25 changes: 0 additions & 25 deletions cmd/bm-server/handler/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,31 +146,6 @@ func checkOrganisation(input inputCreateAccount, w http.ResponseWriter) (*bmcryp
return &orgInfo.PublicKey, nil
}

// RetrieveOrganisation is the handler that will retrieve organisation settings
func RetrieveOrganisation(w http.ResponseWriter, req *http.Request) {
haddr, err := hash.NewFromHash(mux.Vars(req)["addr"])
if err != nil {
httputils.ErrorOut(w, http.StatusBadRequest, "incorrect address")
return
}

// Check if account exists
ar := container.Instance.GetAccountRepo()
if !ar.Exists(*haddr) {
httputils.ErrorOut(w, http.StatusNotFound, "address not found")
return
}

settings, err := ar.FetchOrganisationSettings(*haddr)
if err != nil {
httputils.ErrorOut(w, http.StatusNotFound, "organisation settings not found")
return
}

// Return public keys
httputils.JSONOut(w, http.StatusOK, settings)
}

// RetrieveKeys is the handler that will retrieve public keys directly from the mail server
func RetrieveKeys(w http.ResponseWriter, req *http.Request) {
haddr, err := hash.NewFromHash(mux.Vars(req)["addr"])
Expand Down
18 changes: 9 additions & 9 deletions cmd/bm-server/internal/account/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@

package account

// Folder constants
const (
// BoxRoot is the root box when we want to add boxes without a parent ID
BoxRoot = iota
// BoxInbox is the mandatory inbox where all incoming messages are stored
BoxInbox // Always box 1
// BoxOutbox is the mandatory outbox where send messages are stored
BoxOutbox // Always box 2
// BoxTrash is the mandatory trashcan where deleted messages are stored (before actual deletion)
BoxTrash // Always box 3
BoxInbox = iota + 1 // BoxInbox is the mandatory inbox where all incoming messages are stored
BoxOutbox // BoxOutbox is the mandatory outbox where send messages are stored
BoxTrash // BoxTrash is the mandatory trashcan where deleted messages are stored (before actual deletion)
)

// MandatoryBoxes is a list of all boxes that are mandatory. Makes it easier to range on them
var MandatoryBoxes = []int{BoxInbox, BoxOutbox, BoxTrash}
var MandatoryBoxes = []int{
BoxInbox,
BoxOutbox,
BoxTrash,
}

// MaxMandatoryBoxID is the largest box that must be present. Everything below this box (including this box) is
// mandatory and cannot be removed.
Expand Down
3 changes: 1 addition & 2 deletions cmd/bm-server/internal/account/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ import (
)

const (
keysFile = ".keys.json"
organisationFile = ".organisation.json"
keysFile = ".keys.json"
)

// PubKeys holds a list of public keys
Expand Down
7 changes: 0 additions & 7 deletions cmd/bm-server/internal/account/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ type Repository interface {
KeyRepository
BoxRepository
MessageRepository
OrganisationRepository
}

// AddressRepository creates, checks or deletes complete accounts. Address is not the correct word for this.
Expand All @@ -84,12 +83,6 @@ type KeyRepository interface {
FetchKeys(addr hash.Hash) ([]bmcrypto.PubKey, error)
}

// OrganisationRepository gets and sets organisation settings into an account
type OrganisationRepository interface {
StoreOrganisationSettings(addr hash.Hash, settings OrganisationSettings) error
FetchOrganisationSettings(addr hash.Hash) (*OrganisationSettings, error)
}

// BoxRepository deals with message boxes insides an account
type BoxRepository interface {
CreateBox(addr hash.Hash, parentBox int) error
Expand Down
1 change: 0 additions & 1 deletion cmd/bm-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ func setupRouter() *mux.Router {
publicRouter.HandleFunc("/", handler.HomePage).Methods("GET")
publicRouter.HandleFunc("/account", handler.CreateAccount).Methods("POST")
publicRouter.HandleFunc("/account/{addr:[A-Za-z0-9]{64}}/keys", handler.RetrieveKeys).Methods("GET")
publicRouter.HandleFunc("/account/{addr:[A-Za-z0-9]{64}}/org", handler.RetrieveOrganisation).Methods("GET")

// Server to server message upload
publicRouter.HandleFunc("/ticket", handler.GetServerToServerTicket).Methods("POST")
Expand Down
28 changes: 28 additions & 0 deletions internal/api/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,34 @@ func (api *API) GetPublicKey(addr hash.Hash) (string, error) {
return output.PublicKey, nil
}

// Activate will undelete / activate an account on the resolver again
func (api *API) Activate(info vault.AccountInfo) error {
resp, statusCode, err := api.PostJSON("/account/"+info.Address.Hash().String()+"/undelete", nil)
if err != nil {
return err
}

if statusCode < 200 || statusCode > 299 {
return GetErrorFromResponse(resp)
}

return nil
}

// Deactivate will delete / deactivate an account on the resolver
func (api *API) Deactivate(info vault.AccountInfo) error {
resp, statusCode, err := api.PostJSON("/account/"+info.Address.Hash().String()+"/delete", nil)
if err != nil {
return err
}

if statusCode < 200 || statusCode > 299 {
return GetErrorFromResponse(resp)
}

return nil
}

// CreateAccount creates new account on server
func (api *API) CreateAccount(info vault.AccountInfo, token string) error {
type inputCreateAccount struct {
Expand Down
22 changes: 6 additions & 16 deletions internal/container/bitmaelum.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,14 @@ import (
"github.com/bitmaelum/bitmaelum-suite/internal/webhook"
)

// API keys
const (
// APIKey key
APIKey = "api-key"

// AuthKey key
AuthKey = "auth-key"

// Resolver key
Resolver = "resolver"

// Subscription key
APIKey = "api-key"
AuthKey = "auth-key"
Resolver = "resolver"
Subscription = "subscription"

// Ticket key
Ticket = "ticket"

// Webhook key
Webhook = "webhook"
Ticket = "ticket"
Webhook = "webhook"
)

// Instance is the main bitmaelum service container
Expand Down
8 changes: 4 additions & 4 deletions internal/container/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ func setupResolverService() (interface{}, error) {
if config.Server.Resolver.Sqlite.Enabled {
r, err := getSQLiteRepository(config.Server.Resolver.Sqlite.Dsn)
if err == nil {
_ = repo.Add(r)
_ = repo.(*resolver.ChainRepository).Add(r)
}
}

// We add either the client or the server resolver
if config.Client.Resolver.Remote.Enabled {
_ = repo.Add(*getRemoteRepository(config.Client.Resolver.Remote.URL, config.Client.Server.DebugHTTP, config.Client.Resolver.Remote.AllowInsecure))
_ = repo.(*resolver.ChainRepository).Add(*getRemoteRepository(config.Client.Resolver.Remote.URL, config.Client.Server.DebugHTTP, config.Client.Resolver.Remote.AllowInsecure))
}
if config.Server.Resolver.Remote.Enabled {
_ = repo.Add(*getRemoteRepository(config.Server.Resolver.Remote.URL, false, config.Server.Resolver.Remote.AllowInsecure))
_ = repo.(*resolver.ChainRepository).Add(*getRemoteRepository(config.Server.Resolver.Remote.URL, false, config.Server.Resolver.Remote.AllowInsecure))
}
if config.Bridge.Resolver.Remote.Enabled {
_ = repo.Add(*getRemoteRepository(config.Bridge.Resolver.Remote.URL, false, config.Bridge.Resolver.Remote.AllowInsecure))
_ = repo.(*resolver.ChainRepository).Add(*getRemoteRepository(config.Bridge.Resolver.Remote.URL, false, config.Bridge.Resolver.Remote.AllowInsecure))
}

resolveService = resolver.KeyRetrievalService(repo)
Expand Down
Loading

0 comments on commit 01712e7

Please sign in to comment.