@@ -17,20 +17,20 @@ type MongoClient struct {
1717 Database * mongo.Database
1818}
1919
20- func GetMongoDb () (* MongoClient , error ) {
20+ func GetMongoDb (ctx context. Context ) (* MongoClient , error ) {
2121 mongoURI := os .Getenv ("MONGO_URL" )
2222 if mongoURI == "" {
2323 return nil , errors .New ("MONGO_URL is not set" )
2424 }
2525 clientOptions := options .Client ().ApplyURI (mongoURI )
2626
27- mongoClient , err := mongo .Connect (nil , clientOptions )
27+ mongoClient , err := mongo .Connect (ctx , clientOptions )
2828 if err != nil {
2929 log .Fatal (err )
3030 return nil , err
3131 }
3232
33- err = mongoClient .Ping (nil , nil )
33+ err = mongoClient .Ping (ctx , nil )
3434 if err != nil {
3535 log .Fatal (err )
3636 return nil , err
@@ -45,7 +45,7 @@ func GetMongoDb() (*MongoClient, error) {
4545}
4646
4747func (db * MongoClient ) Disconnect (ctx context.Context ) error {
48- return db .Disconnect (ctx )
48+ return db .Database . Client (). Disconnect (ctx )
4949}
5050
5151
@@ -62,6 +62,29 @@ func (r *TppMongoRepository) GetTpp(ctx context.Context, id string) (*models.TPP
6262 return tpp , nil
6363}
6464
65+ func (r * TppMongoRepository ) GetRootCertificates (ctx context.Context ) ([]string , error ) {
66+ // Get all certificates from the "certs" collection for now
67+ cursor , err := r .db .Collection ("certs" ).Find (ctx , bson.M {})
68+ if err != nil {
69+ return nil , err
70+ }
71+ defer cursor .Close (ctx )
72+
73+ var roots []string
74+ for cursor .Next (ctx ) {
75+ // TODO: move models from the tools package to the app/models package
76+ var tpp models.ParsedCert
77+ if err := cursor .Decode (& tpp ); err != nil {
78+ return nil , err
79+ }
80+ roots = append (roots , tpp .Pem )
81+ }
82+ if err := cursor .Err (); err != nil {
83+ return nil , err
84+ }
85+ return roots , nil
86+ }
87+
6588func NewTppMongoRepository (db * mongo.Database ) * TppMongoRepository {
6689 return & TppMongoRepository {db : db }
6790}
0 commit comments