volixer 5 månader sedan
incheckning
cba634939d
10 ändrade filer med 261 tillägg och 0 borttagningar
  1. 33 0
      config/ovh.go
  2. 15 0
      go.mod
  3. 28 0
      go.sum
  4. 80 0
      handlers/sshkey_handler.go
  5. 26 0
      main.go
  6. 20 0
      middleware/logging.go
  7. 9 0
      models/sshkey.go
  8. 13 0
      ovh.conf
  9. 19 0
      routes/routes.go
  10. 18 0
      routes/sshkey.go

+ 33 - 0
config/ovh.go

@@ -0,0 +1,33 @@
+package config
+
+import (
+	"log"
+	"path/filepath"
+
+	"github.com/ovh/go-ovh/ovh"
+)
+
+var OVHClient *ovh.Client
+
+// InitOVHClient initializes the OVH client using the configuration file
+func InitOVHClient() {
+	// Get absolute path to ovh.conf in current directory
+	configPath, err := filepath.Abs("./ovh.conf")
+	if err != nil {
+		log.Fatalf("Error getting config path: %v", err)
+	}
+
+	// Create client using config file
+	client, err := ovh.NewClient(
+		"ovh-eu", // Endpoint
+		"",       // Application key (will be loaded from config)
+		"",       // Application secret (will be loaded from config)
+		"",       // Consumer key (will be loaded from config)
+	)
+	if err != nil {
+		log.Fatalf("Error creating OVH client: %v", err)
+	}
+
+	log.Printf("OVH client initialized with config from: %s", configPath)
+	OVHClient = client
+}

+ 15 - 0
go.mod

@@ -0,0 +1,15 @@
+module byom-infra-api
+
+go 1.23.5
+
+require github.com/gorilla/mux v1.8.1
+
+require (
+	github.com/golang/protobuf v1.5.3 // indirect
+	github.com/ovh/go-ovh v1.6.0 // indirect
+	golang.org/x/net v0.22.0 // indirect
+	golang.org/x/oauth2 v0.18.0 // indirect
+	google.golang.org/appengine v1.6.7 // indirect
+	google.golang.org/protobuf v1.31.0 // indirect
+	gopkg.in/ini.v1 v1.67.0 // indirect
+)

+ 28 - 0
go.sum

@@ -0,0 +1,28 @@
+github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
+github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
+github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
+github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
+github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
+github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
+github.com/ovh/go-ovh v1.6.0 h1:ixLOwxQdzYDx296sXcgS35TOPEahJkpjMGtzPadCjQI=
+github.com/ovh/go-ovh v1.6.0/go.mod h1:cTVDnl94z4tl8pP1uZ/8jlVxntjSIf09bNcQ5TJSC7c=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
+golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
+golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
+golang.org/x/oauth2 v0.18.0 h1:09qnuIAgzdx1XplqJvW6CQqMCtGZykZWcXzPMPUusvI=
+golang.org/x/oauth2 v0.18.0/go.mod h1:Wf7knwG0MPoWIMMBgFlEaSUDaKskp0dCfrlJRJXbBi8=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
+google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
+google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
+google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
+google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
+google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
+gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
+gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=

+ 80 - 0
handlers/sshkey_handler.go

@@ -0,0 +1,80 @@
+package handlers
+
+import (
+	"byom-infra-api/config"
+	"encoding/json"
+	"log"
+	"net/http"
+
+	"github.com/gorilla/mux"
+)
+
+type SSHKey struct {
+	KeyName string `json:"keyName"`
+	Key     string `json:"key"`
+}
+
+type SSHKeyHandler struct{}
+
+func NewSSHKeyHandler() *SSHKeyHandler {
+	return &SSHKeyHandler{}
+}
+
+// GetSSHKeys handles the retrieval of all SSH keys
+func (h *SSHKeyHandler) GetSSHKeys(w http.ResponseWriter, r *http.Request) {
+	log.Printf("Handling GET request for all SSH keys from %s", r.RemoteAddr)
+
+	// First, get the list of SSH key IDs
+	var keyIDs []string
+	err := config.OVHClient.Get("/me/sshKey", &keyIDs)
+	if err != nil {
+		log.Printf("Error fetching SSH key IDs: %v", err)
+		http.Error(w, "Failed to fetch SSH keys: "+err.Error(), http.StatusInternalServerError)
+		return
+	}
+
+	// Then fetch details for each key
+	var sshKeys []SSHKey
+	for _, id := range keyIDs {
+		var key SSHKey
+		err := config.OVHClient.Get("/me/sshKey/"+id, &key)
+		if err != nil {
+			log.Printf("Error fetching details for SSH key %s: %v", id, err)
+			continue
+		}
+		sshKeys = append(sshKeys, key)
+	}
+
+	log.Printf("Successfully retrieved %d SSH keys", len(sshKeys))
+
+	w.Header().Set("Content-Type", "application/json")
+	if err := json.NewEncoder(w).Encode(sshKeys); err != nil {
+		log.Printf("Error encoding SSH keys response: %v", err)
+		http.Error(w, "Failed to encode response", http.StatusInternalServerError)
+		return
+	}
+}
+
+// GetSSHKeyByID handles the retrieval of a specific SSH key by ID
+func (h *SSHKeyHandler) GetSSHKeyByID(w http.ResponseWriter, r *http.Request) {
+	vars := mux.Vars(r)
+	keyID := vars["id"]
+	log.Printf("Handling GET request for SSH key ID: %s from %s", keyID, r.RemoteAddr)
+
+	var sshKey SSHKey
+	err := config.OVHClient.Get("/me/sshKey/"+keyID, &sshKey)
+	if err != nil {
+		log.Printf("Error fetching SSH key %s: %v", keyID, err)
+		http.Error(w, "Failed to fetch SSH key: "+err.Error(), http.StatusInternalServerError)
+		return
+	}
+
+	log.Printf("Successfully retrieved SSH key: %s", keyID)
+
+	w.Header().Set("Content-Type", "application/json")
+	if err := json.NewEncoder(w).Encode(sshKey); err != nil {
+		log.Printf("Error encoding SSH key response: %v", err)
+		http.Error(w, "Failed to encode response", http.StatusInternalServerError)
+		return
+	}
+}

+ 26 - 0
main.go

@@ -0,0 +1,26 @@
+package main
+
+import (
+	"byom-infra-api/config"
+	"byom-infra-api/routes"
+	"log"
+	"net/http"
+
+	"github.com/gorilla/mux"
+)
+
+func main() {
+	// Initialize OVH client
+	config.InitOVHClient()
+
+	r := mux.NewRouter()
+
+	// Setup routes
+	routes.SetupRoutes(r)
+
+	// Start server
+	log.Println("Server starting on port 8080...")
+	if err := http.ListenAndServe(":8080", r); err != nil {
+		log.Fatal(err)
+	}
+}

+ 20 - 0
middleware/logging.go

@@ -0,0 +1,20 @@
+package middleware
+
+import (
+	"log"
+	"net/http"
+	"time"
+)
+
+// LoggingMiddleware logs information about each HTTP request
+func LoggingMiddleware(next http.Handler) http.Handler {
+	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		start := time.Now()
+
+		log.Printf("Started %s %s from %s", r.Method, r.URL.Path, r.RemoteAddr)
+
+		next.ServeHTTP(w, r)
+
+		log.Printf("Completed %s %s in %v", r.Method, r.URL.Path, time.Since(start))
+	})
+}

+ 9 - 0
models/sshkey.go

@@ -0,0 +1,9 @@
+package models
+
+type SSHKey struct {
+	ID          string `json:"id"`
+	Name        string `json:"name"`
+	PublicKey   string `json:"public_key"`
+	Fingerprint string `json:"fingerprint"`
+	CreatedAt   string `json:"created_at"`
+}

+ 13 - 0
ovh.conf

@@ -0,0 +1,13 @@
+# https://github.com/ovh/go-ovh?tab=readme-ov-file#application-keyapplication-secret
+# https://www.ovh.com/auth/api/createToken?GET=/*&PUT=/*&POST=/*&DELETE=/*
+[default]
+; general configuration: default endpoint
+endpoint=ovh-eu
+
+[ovh-eu]
+; LFWD API APP
+; configuration specific to 'ovh-eu' endpoint
+application_key=4c8d3955e6e4cf8b
+application_secret=0a9155985cd484d2fe4036ca14750fa2
+consumer_key=b4bafb0786b3ac0e4f9fb873353faab1
+

+ 19 - 0
routes/routes.go

@@ -0,0 +1,19 @@
+package routes
+
+import (
+	"byom-infra-api/middleware"
+
+	"github.com/gorilla/mux"
+)
+
+// SetupRoutes configures all the routes for the application
+func SetupRoutes(r *mux.Router) {
+	// Add logging middleware
+	r.Use(middleware.LoggingMiddleware)
+
+	// SSH Key routes
+	r.HandleFunc("/sshkey", GetSSHKeys).Methods("GET")
+	r.HandleFunc("/sshkey/{id}", GetSSHKeyByID).Methods("GET")
+
+	// Add other routes here as needed
+}

+ 18 - 0
routes/sshkey.go

@@ -0,0 +1,18 @@
+package routes
+
+import (
+	"byom-infra-api/handlers"
+	"net/http"
+)
+
+var sshKeyHandler = handlers.NewSSHKeyHandler()
+
+// GetSSHKeys forwards the request to the SSHKeyHandler
+func GetSSHKeys(w http.ResponseWriter, r *http.Request) {
+	sshKeyHandler.GetSSHKeys(w, r)
+}
+
+// GetSSHKeyByID forwards the request to the SSHKeyHandler
+func GetSSHKeyByID(w http.ResponseWriter, r *http.Request) {
+	sshKeyHandler.GetSSHKeyByID(w, r)
+}