Go to file
mandi 4a1a5a02af Renamed package to fit new Project Name 2022-05-12 13:56:52 +02:00
test Changed Project Name from godovecot to modDovecot 2022-05-12 13:51:50 +02:00
types Renamed xmlPacket.go struct file 2022-01-25 00:09:11 +01:00
vendor Initial logging implementation 2022-04-25 18:02:20 +02:00
.gitignore Changed .gitignore 2022-05-12 13:49:31 +02:00
README.md Added description in README.md 2022-04-25 20:43:29 +02:00
go.mod Changed Project Name from godovecot to modDovecot 2022-05-12 13:51:50 +02:00
go.sum Initial logging implementation 2022-04-25 18:02:05 +02:00
godovecot.go Renamed package to fit new Project Name 2022-05-12 13:56:52 +02:00

README.md

godovecot

Functions

CreatePacket

See for Details

https://docs.plesk.com/en-US/obsidian/api-rpc/about-xml-api/reference/managing-mail/mail-account-settings.34481/

Usage

NewServer

For configaration issues the server is separated in two structs

struct content usage
server all needed pre-defined configdata passing all config parameters in e.g main.go
DovecotServer server and ApiKey which is generated by NewServer Adding ApiKey struct on intialisation with NewServer()

For initiasation NewServer(ds MailServer) is umplemented as constructor passing a MailServer struct ds containing all pre-defined parameters

Parameter Type Description Purpose
ApiHost string The URL to the plesk host Account backup/restore
ApiUser string Plesk user with API usage provileges Account backup/restore
Password string Password of API user Account backup/restore
SSHHost string SSH host Maildir backup/restore
SSHPort string Port of SSH server Maildir backup/restore
SSHUser string SSH user Maildir backup/restore
SSHPassword string SSH user password Maildir backup/restore
MaildirPath string Path of dovecot mail directory (Maildir) Maildir backup/restore
BakupMailsPath string Local folder path for backup (destination) Maildir backup/restore
BackupAccountsPath string Local folder path for backup of accounts information Account backup/restore
MailDirOwner string User of Maildir (used on chown) Account restore
MailDirGroup string User of Maildir (used on chown) Account restore

NewServer(ds MailServer) gets MailServer struct and performs following actions

  • Creation of new server struct instance srv
  • Calls s.logIn() to create ApiKey

Returns: server struct instance srv

Logging

Problem statement

To implement logging using a module (like logrus) the functions must be implemented either in main.go or in dovecot using exactly the choosen module. Using an other module later would be a huge effort for refactoring Solution To be able to use any logging api you want logging is implemented as callbacks including seperate functions for

  • Info
  • Error
  • Warn
  • Fatal

A struct containing the callbacks for above log-levels is created as godovecot.Logger. This is allocated at server-creation in the followimg way

f, err := os.OpenFile("testlogrus.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
    fmt.Printf("error opening file: %v", err)
}

logger := logrus.New()
logger.SetOutput(f)

log := log.Default()

	sl := dovecot.Logger{

		LogError: func(e ...interface{}) {
			//log.Println(e)
			logger.Error(e)
		},
		LogInfo: func(i ...interface{}) {
			//log.Println(i)
			logger.Info(i)
		},
		LogWarn: func(w ...interface{}) {
			//log.Println(w)
			logger.Warn(w)
		},
	}

	ds := dovecot.MailServer{
		ApiConfig: &dovecot.ApiConfig{
			ApiHost:  "https://plesk.ververis.eu",
			ApiUser:  "admin",
			Password: "Sektor@mailD2312",
		},
		SSHConfig: &dovecot.SSHConfig{
			SSHHost:             "89.22.111.212",
			SSHPort:             "22",
			SSHUser:             "root",
			LocalPrivateKeyPath: "/Users/mandi/.ssh/id_rsa",
			LocalKnownHostPath:  "/Users/mandi/.ssh/known_hosts",
		},
		MaildirPath:        "/var/qmail/mailnames/ververis.eu",
		BakupMailsPath:     "sync",
		BackupAccountsPath: "sync/accounts",
		MailDirOwner:       "popuser",
		MailDirGroup:       "popuser",

		Logger: &sl,
	}

Above example shows implementation using sirupsen/logrus https://github.com/sirupsen/logrus

RestoreMail(a ...IPacket)

ResroreMail(a ...IPacket) is implemented as variadic to be able to pass parameters as different types implementing the IPacket interface:

  • PacketValues
    • For passing strings of the account to create
  • FullPacket
    • For passing directing a created xml Packet
    • Packet needs to be passed by appending the slice directly
    fullPacket.Packets = append(fullPacket.Packets, &newPacket) 
    
  • FilePacket
    • For passing the path to a .json backup file

logIn()

The logIn is done in several steps

  • Checks if serialized server struct is avalaible
    • If serialization is available
      • Checks if ApiKey is set
        • If it is set the is passed to used server struct
      • If not set
        • callApiKey() is called to get a valid API-Key
  • If serialization is not available
    • callApiKey() is called to get a valid API-Key
    • Called API-Key is passed to the used server instance
    • User server instance is serialized to file

Information

Keys

Needs ssh keys.

To genarate keys

  • On local machine
    ssh-keygen -t rsa -b 4096
    
    ssh-copy-id root@89.22.111.212
    
    • Enter ssh-password to "register" ssh-key on server

The module needs also to run in a Docker container. To do so the container -as a unique client- needs to have it's "own" ssh-key.
There are two ways to to that.

  1. Create an ssh key on first start of the program For this the steps described in the section Keys must be implemented in Go to store an ssh key to the container and to "register" this key to ther server
  2. Use the ssh key of the host For using .ssh keys of the host in Docker see here:
    https://medium.com/trabe/use-your-local-ssh-keys-inside-a-docker-container-ea1d117515dc

Restore

  • Upload synced folder to server
  • Change ownership of uploaded folder to dovecot user chown -R popuser:popuser ververis.eu

Backup accounts

  • Dialing to server with ssh
  • Creating output of dovecot mail accounts including passwords /usr/local/psa/admin/sbin/mail_auth_view
  • Read in the output as .csv and parse to an array of accounts
  • Save file as .json

Uses libraries / modules