server/admin/pki/team.go
Pierre-Olivier Mercier 771627a0da pki: fix team association, complement to 68e5c4cd2b
The fix introduced in the referenced commit was not working.

This time, it has been tested with the following commands:

	# Associate all certificate to a team
	curl http://localhost:8081/api/certs/ | jq -r .[].id | while read CERTID; do curl -X PUT -d '{"id_team":1}' http://localhost:8081/api/certs/$CERTID; done

	# For each certificate associated with the team, try to connect to the server with each certificate. Report failing certificates.
	curl -s http://localhost:8081/api/teams/1/certificates | jq -r '.[] | .id + " " + .password' | while read CERTID PASSWORD; do curl -sf --cert-type P12 --cert $CERTID.p12:$PASSWORD https://fic.srs.epita.fr/my.json > /dev/null || echo $CERTID; done
2019-02-06 03:40:49 +01:00

69 lines
1.7 KiB
Go

package pki
import (
"fmt"
"io/ioutil"
"math"
"os"
"path"
"strconv"
"strings"
)
const SymlinkPrefix = "_AUTH_ID_"
func GetCertificateAssociation(serial uint64) string {
return fmt.Sprintf(SymlinkPrefix + "%0[2]*[1]X", serial, int(math.Ceil(math.Log2(float64(serial))/8)*2))
}
func GetAssociations(dirname string) (assocs []string, err error) {
if ds, errr := ioutil.ReadDir(dirname); err != nil {
return nil, errr
} else {
for _, d := range ds {
if d.Mode() & os.ModeSymlink == os.ModeSymlink {
assocs = append(assocs, d.Name())
}
}
return
}
}
func GetTeamSerials(dirname string, id_team int64) (serials []uint64, err error) {
// As futher comparaisons will be made with strings, convert it only one time
str_tid := fmt.Sprintf("%d", id_team)
var assocs []string
if assocs, err = GetAssociations(dirname); err != nil {
return
} else {
for _, assoc := range assocs {
var tid string
if tid, err = os.Readlink(path.Join(dirname, assoc)); err == nil && tid == str_tid && strings.HasPrefix(assoc, SymlinkPrefix) {
if serial, err := strconv.ParseUint(assoc[9:], 16, 64); err == nil {
serials = append(serials, serial)
}
}
}
}
return
}
func GetTeamAssociations(dirname string, id_team int64) (teamAssocs []string, err error) {
// As futher comparaisons will be made with strings, convert it only one time
str_tid := fmt.Sprintf("%d", id_team)
var assocs []string
if assocs, err = GetAssociations(dirname); err != nil {
return
} else {
for _, assoc := range assocs {
var tid string
if tid, err = os.Readlink(path.Join(dirname, assoc)); err == nil && tid == str_tid && !strings.HasPrefix(assoc, SymlinkPrefix) {
teamAssocs = append(teamAssocs, assoc)
}
}
}
return
}