Provide enrollment, elevated install, posture checks, keystore, and system-tray helpers shared by the probo-agent binary. Signed-off-by: Ludovic Vielle <ludovic@probo.com>
78 lines
2.6 KiB
Go
78 lines
2.6 KiB
Go
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
|
|
package deviceagent
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestClientExchangeEnrollmentToken(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const wantAPIKey = "device-api-key-secret"
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
assert.Equal(t, http.MethodPost, r.Method)
|
|
assert.Equal(t, "/api/agent/v1/enroll", r.URL.Path)
|
|
assert.Empty(t, r.Header.Get("Authorization"))
|
|
|
|
var body struct {
|
|
Token string `json:"token"`
|
|
}
|
|
assert.NoError(t, json.NewDecoder(r.Body).Decode(&body))
|
|
assert.Equal(t, "enroll-token", body.Token)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{
|
|
"api_key": wantAPIKey,
|
|
}))
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
|
|
client := NewClient(srv.URL, "", "probo-agent/test")
|
|
|
|
apiKey, err := client.ExchangeEnrollmentToken(context.Background(), "enroll-token")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, wantAPIKey, apiKey)
|
|
}
|
|
|
|
func TestClientExchangeEnrollmentTokenUnauthorized(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
|
|
client := NewClient(srv.URL, "", "probo-agent/test")
|
|
|
|
_, err := client.ExchangeEnrollmentToken(context.Background(), "used-token")
|
|
require.Error(t, err)
|
|
assert.True(t, IsUnauthorized(err))
|
|
}
|