I'm developing a custom plugin which is a PKI backend with import to other software functionality (https://github.com/Venafi/vault-pki-monitor-venafi).
This plugin has logic to start import queue in go routine, when role is created, and monitor for new certificates in the role to import them into external system:
- https://github.com/Venafi/vault-pki-monitor-venafi/blob/adding-tests-for-import/plugin/pki/path_import_queue.go#L83 - https://github.com/Venafi/vault-pki-monitor-venafi/blob/adding-tests-for-import/plugin/pki/path_roles.go#L606-L610 To be sure that there is only one import go routine for each role I'm creating a lock path and setting it to true: https://github.com/Venafi/vault-pki-monitor-venafi/blob/adding-tests-for-import/plugin/pki/path_import_queue.go#L88-L153 If lock path doesn't exist I'm assuming it is false: https://github.com/Venafi/vault-pki-monitor-venafi/blob/adding-tests-for-import/plugin/pki/path_import_queue.go#L112-L114
It's all working fine when I'm testing it with vault binary. But when I tried to implement tests using vault.NewTestCluster - https://github.com/Venafi/vault-pki-monitor-venafi/blob/adding-tests-for-import/plugin/pki/path_import_queue_test.go#L16-L63 I got this error:
2018-11-08T15:24:58.004+0300 [DEBUG] core: forwarding: error sending echo request to active node: error="rpc error: code = DeadlineExceeded desc = context deadline exceeded"
2018/11/08 15:25:23 Locking import mutex on backend to safely change data for import lock
2018/11/08 15:25:23 Getting import lock for path import-queue-lock/import
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x112863d]
goroutine 557 [running]:
github.com/Venafi/vault-pki-monitor-venafi/plugin/pki.(*backend).importToTPP(0xc4202d3360, 0xc4202ebece, 0x6, 0x14fa8e0, 0xc420040170, 0xc420295a40)
/home/user/src/go/src/github.com/Venafi/vault-pki-monitor-venafi/plugin/pki/path_import_queue.go:105 +0x2fd
created by github.com/Venafi/vault-pki-monitor-venafi/plugin/pki.(*backend).pathRoleCreate
/home/user/src/go/src/github.com/Venafi/vault-pki-monitor-venafi/plugin/pki/path_roles.go:609 +0x2a6b
Here is the place where I'm getting error (this is works fine when running with vault binary, only tests are failing):
log.Printf("Getting import lock for path %s", lockPath)
var importLockEntry *logical.StorageEntry
importLockEntry, err = req.Storage.Get(ctx, lockPath)
if err != nil {
log.Printf("Unable to get lock import for role %s:
%s
", roleName, err)
unlock()
return
}
if importLockEntry == nil || importLockEntry.Value == nil || len(importLockEntry.Value) == 0 {
log.Println("Role lock is empty, assuming it is false")
importLocked = false
} else {
log.Printf("Got from storage %s", string(importLockEntry.Value))
il := string(importLockEntry.Value)
log.Printf("Parsing %s to bool", il)
importLocked, err = strconv.ParseBool(il)
if err != nil {
log.Printf("Unable to parse lock import %s to bool for role %s:
%s
", il, roleName, err)
unlock()
return
}
}
This is how it looks in debugger: https://13027918075534280191.googlegroups.com/attach/5de3757908e52/Screenshot%20from%202018-11-09%2013-24-52.png
importLockEntry is nil but I'm checking it before usage so it should be fine as I understand. However I'm getting panic right on the https://github.com/Venafi/vault-pki-monitor-venafi/blob/adding-tests-for-import/plugin/pki/path_import_queue.go#L105 - the line where I'm trying to set variable.
So, what I'm missing? Why when I run my plugin with vault binary it's working fine and when I'm trying to run i with vault.NewTestCluster it panics?