Add authentication and few environment variables to deal with conflicts regarding names of IPE on CSE

Merge request reports

Loading
+0 −76
Changes for go-apps/meep-ipe/oneM2M/createSubscription.go: 0 added lines, 76 removed lines.
Original line number Diff line number Diff line
package onem2m

import (
	"bytes"
	"crypto/tls"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
)

// CreateSubscription creates a Subscription resource on the specified container
//
// This function creates a Subscription resource that enables the oneM2M platform
// to send notifications to a specified endpoint when events occur on the container
// (e.g., when new ContentInstances are created).
//
// Reference: oneM2M TS-0001 - Functional Architecture
//
// @param platformURL The base URL of the oneM2M CSE platform
// @param cseName The name of the Common Services Entity (CSE)
// @param appName The resource name for the application
// @param containerName The name of the container to subscribe to
// @return error Returns nil on success, or an error if subscription creation fails
func CreateSubscription(platformURL, cseName, appName, containerName string) error {
	subscriptionURL := fmt.Sprintf("%s/%s/%s/%s", platformURL, cseName, appName, containerName)

	target := subscriptionURL
	// Subscription resource body
	body := map[string]interface{}{
		"m2m:sub": map[string]interface{}{
			"rn":  "DataSyncSub",
			"nu":  []string{"https://192.168.20.163:9999/notifications"},
			"nct": 1,
			"enc": map[string]interface{}{
				"net": []int{3},
			},
		},
	}
	jsonBody, err := json.Marshal(body)
	if err != nil {
		return fmt.Errorf("failed to marshal subscription body: %v", err)
	}

	adminUser := "CAdmin"
	adminSecret := "ikram123"
	authString := fmt.Sprintf("%s:%s", adminUser, adminSecret)
	authHeader := "Basic " + base64.StdEncoding.EncodeToString([]byte(authString))

	req, err := http.NewRequest("POST", target, bytes.NewBuffer(jsonBody))
	if err != nil {
		return fmt.Errorf("failed to create subscription request: %v", err)
	}
	req.Header.Set("X-M2M-Origin", adminUser)
	req.Header.Set("X-M2M-RI", "req-admin")
	req.Header.Set("Authorization", authHeader)
	req.Header.Set("X-M2M-RVI", "5")
	req.Header.Set("Content-Type", "application/json;ty=23")

	// Skip TLS verification for development/testing
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	client := &http.Client{Transport: tr}
	resp, err := client.Do(req)
	if err != nil {
		return fmt.Errorf("failed to send subscription request: %v", err)
	}
	defer resp.Body.Close()
	respBody, _ := io.ReadAll(resp.Body)
	if resp.StatusCode != 201 && resp.StatusCode != 409 { // 409: Already exists
		return fmt.Errorf("subscription creation failed: %s", string(respBody))
	}
	return nil
}
+8 −0
Changes for go-apps/meep-ipe/oneM2M/oneM2m_init.go: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -3,14 +3,22 @@ package onem2m
import (
	"fmt"
	"log"

	"meep-ipe/utils"
)

var config = make(map[string]interface{})

func Init(configmap map[string]interface{}) {
	config = configmap

	user, _ := config["CSE-USER"].(string)
	password, _ := config["CSE-PASSWORD"].(string)
	utils.SetCSECredentials(user, password)

	log.Println("oneM2M configuration initialized:", config)
}

// NodeName returns the rn of this IPE's node on the CSE. It is suffixed with the
// sandbox name so multiple sandboxes can share the same CSE without conflicts.
func NodeName() string {
+13 −1
Changes for go-apps/meep-ipe/utils/onem2m_req.go: 13 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -11,6 +11,15 @@ import (
	"github.com/google/uuid"
)

// CSE HTTP Basic auth credentials; when cseUser is empty no Authorization header is sent
var cseUser, csePassword string

// SetCSECredentials sets the HTTP Basic auth credentials used for all oneM2M requests
func SetCSECredentials(user, password string) {
	cseUser = user
	csePassword = password
}

// MakeOneM2MRequest creates and sends a oneM2M HTTP request
//
// This function creates and sends an HTTP request to a oneM2M CSE platform endpoint.
@@ -42,7 +51,10 @@ func MakeOneM2MRequest(method, url, body string, ty int, originator string) (*ht
		"X-M2M-RI":     reqID,
		"X-M2M-RVI":    "5",
		"Accept":       "application/json",
		"Authorization": "Basic " + base64.StdEncoding.EncodeToString([]byte("CAdmin:ikram123")),
	}

	if cseUser != "" {
		headers["Authorization"] = "Basic " + base64.StdEncoding.EncodeToString([]byte(cseUser+":"+csePassword))
	}

	if method == "POST" {
+13 −1
Changes for go-apps/meep-ipe/config.env: 13 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -74,7 +74,19 @@ MEEP_CSE_PORT=CSE-PORT
MEEP_CSE_ID=

# CSE name. Default if empty: "laboai-cse-in"
MEEP_CSE_NAME=
MEEP_CSE_NAME=laboai-cse-in

# Base AE-ID (originator) used to register on the CSE. Should start with "C"
# (or "S") per oneM2M originator rules. MEEP_LOCATION_NAME and
# MEEP_SANDBOX_NAME are appended automatically, e.g.
#   CMecIPE -> CMecIPE-mep1-sbxoon0fww
# Default if empty: "CMecIPE"
MEEP_AE_ID=CMecIPE

# HTTP Basic auth credentials for the CSE. Leave both empty if the CSE has
# no authorization enabled (no Authorization header is sent).
MEEP_CSE_USER=
MEEP_CSE_PASSWORD=


# ==============================================================================
+3 −0
Changes for go-apps/meep-ipe/config.yaml.in: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -29,5 +29,8 @@ CSE-PORT: '${CSE_PORT}'
CSE-ID: '${CSE_ID}'
#CSE NAME
CSE-NAME: '${CSE_NAME}'
# CSE HTTP Basic auth credentials (empty = no auth)
CSE-USER: '${CSE_USER}'
CSE-PASSWORD: '${CSE_PASSWORD}'
#AE ID
AE-ID: '${AE_ID}'
 No newline at end of file
Loading
Loading