Skip to main content

Documentation Olfeo OEM

Utiliser le SDK

Lancer et démarrer le SDK

Pour utiliser le SDK, vous devrez disposer d'une instance de Database et de Categorizer.

  • Le categorizer est utilisé pour catégoriser un domaine

  • La Database est utilisé pour obtenir les métadonnées des catégories d'URL ou d'applications.

La meilleure façon de comprendre le fonctionnement du SDK est de regarder le code d'exemple dans ; /src/main.go

Activer la correspondance IP

Si vous souhaitez faire de la catégorisation ou de la détection en utilisant des adresses IP plutôt que des URL, vous devriez utiliser Database.SetIpRangeMatcher pour améliorer les performances.

Fonctions principales

sdk/categorize

Pour instancier une instance de Categorizer, utilisez la fonction NewCategorize depuis ./src/sdk/categorize/handler.go avec une instance de Database

// Categorizer correspond to the main interface that provide the categorization service
type Categorizer interface {
    // GetDomainCategory returns the correct category id of the given domain or url
    GetDomainCategory(ctx context.Context, domain string, urlPath string) (uint32, error)
    // GetDomainApplications returns the application ids of the given domain, in case where the domain is an IP, there can be multiple applications
    GetDomainApplications(ctx context.Context, domain string) ([]uint32, error)
    // GetAdvancedDomainInfo performs the same algorithm as GetDomainInfo but stores intermediate results
    GetAdvancedDomainInfo(ctx context.Context, domain string, urlPath string) (*AdvancedDomainInfo, error)
}
sdk/database

Pour instancier une instance de Database, utilisez la fonction NewDatabase depuis ./src/sdk/database/redis/connect.go avec un context.Context et un redis.UniversalClient.

type Database interface {
    // GetApplicationInfo returns metadata about the give applicationId
    //
    // Querying for a non-existent applicationId returns a sdk.NotInDatabase error
    GetApplicationInfo(ctx context.Context, applicationId uint32) (*ApplicationInfo, error)
    // GetDomainApplicationInfo returns application info about the give domain
    //
    // Querying for an unknown domain returns a sdk.NotInDatabase error
    GetDomainApplicationInfo(ctx context.Context, domain string) (*DomainApplicationInfo, error)
    // GetApplicationCategoryInfo returns metadata about the give applicationCategoryId
    //
    // Querying for a non-existent applicationCategoryId returns a sdk.NotInDatabase error
    GetApplicationCategoryInfo(ctx context.Context, applicationCategoryId uint32) (*ApplicationCategoryInfo, error)
    // GetCategoryInfo returns metadata about the give categoryId
    //
    // Querying for a non-existent categoryId returns a sdk.NotInDatabase error
    GetCategoryInfo(ctx context.Context, categoryId uint32) (*CategoryInfo, error)
    // GetThemeInfo returns metadata about the give categoryId
    //
    // Querying for a non-existent themeId returns a sdk.NotInDatabase error
    GetThemeInfo(ctx context.Context, themeId uint32) (*ThemeInfo, error)
    // GetLogoData returns the byte sequence for the logo (as a 64x64 pixel PNG image)
    //
    // Querying for a non-existant logoId returns a sdk.NotInDatabase error
    GetLogoData(ctx context.Context, logoId uint32) ([]byte, error)
    // GetDomainInfo returns the domain info associated with a given domain
    //
    // Querying for an unknown domain returns a sdk.NotInDatabase error
    GetDomainInfo(ctx context.Context, domain string) (*DomainInfo, error)
    // GetCategoryInfoList return a map of info on all categories in the database
    GetCategoryInfoList(ctx context.Context) (map[uint32]*CategoryInfo, error)
    // GetThemeInfoList returns a map of info on all themes in the database
    GetThemeInfoList(ctx context.Context) (map[uint32]*ThemeInfo, error)
    // GetThemeCategoryIds returns a list of category ids for a given theme
    //
    // Querying for an unknown theme returns an empty list
    GetThemeCategoryIds(ctx context.Context, themeId uint32) ([]uint32, error)
    // GetCategoryIds returns a list of all the categories in the database
    GetCategoryIds(ctx context.Context) ([]uint32, error)
    // GetThemeIds returns a list of all the themes in the database
    GetThemeIds(ctx context.Context) ([]uint32, error)
    // GetIpApplicationIds returns a list of all the applications in the database
    GetIpApplicationIds(ctx context.Context, ip string) ([]uint32, error)
    // GetApplicationCategoryIds returns a list of all the application categories in the database
    GetApplicationCategoryIds(ctx context.Context) ([]uint32, error)
    // GetCategoryApplicationIds returns a list of all the applications in the given application category in the database
    GetCategoryApplicationIds(ctx context.Context, applicationCategoryId uint32) ([]uint32, error)
    // GetApplicationIpRanges returns a list of all IP range pertaining to the applicationId
    GetApplicationIpRanges(ctx context.Context, applicationId uint32) ([]ip_ranges.IpRange, error) 
    // HealthCheck returns an error if the database is not working correctly.
    //
    // The returned error will wrap the actual backend error
    HealthCheck(ctx context.Context) error
}
Détecter des application par leur IP

Le SDK permet la détection de l'appartenance d'une IP à une application SaaS mais son comportement par défaut peut ne pas donner des performances élevées, puisque le comportement par défaut recherche une égalité stricte dans la base plutôt que l'appartenance de l'IP à une plage.

La méthode Database.SetIpRangeMatcher peut être utilisée pour modifier ce comportement.

L'AutoUpdateMatcher, comme son nom l'indique, mettra à jour les plages d'IP automatiquement si des changements interviennent dans la base de données.

ctx := context.Background()
db, err := rdb.NewDatabase(ctx, redisClient)
if err != nil {
    panic("initialize db rdb")
}
autoUpdateMatcher, err := matcher.NewAutoUpdateMatcher(ctx, db)
if err != nil {
    panic("initialize iprange autoUpdateMatcher")
}
db.SetIpRangeMatcher(autoUpdateMatcher)
// Then use `Database.GetIpApplicationIds` as usual.

Si être à jour de façon réactive n'est pas un requis, il peut être desactivé en passant l'option WithoutAutoUpdate au matcher.NewAutoUpdateMatcher

Extraire des plages IP pour une application spécifique

Pour utiliser cette fonctionnalité, vous devez initialiser une structure d'assistance spécifique, qui peut ensuite être interrogée pour les plages IP correspondantes.

Vous trouverez ci-dessous un exemple de code pour initialiser la structure :

ctx := context.Background()
db, err := rdb.NewDatabase(ctx, redisClient)
if err != nil {
    panic("initialize db rdb")
}

ipRangesBuilder, err := matcher.NewAutoUpdateAppsIpRangesBuilder(ctx, db)
if err != nil {
    panic("initialize ipRangesBuilder")
}

ranges, err := ipRangesBuilder.GetIpRanges(applicationId)
// ranges will contain all IpRanges matching the application, if err is nil

Mise à jour automatique des plages IP

Le AutoUpdateAppsIpRangesBuilder met automatiquement à jour les plages IP lorsque la base de données change. Pour désactiver ce comportement, utilisez WithoutAutoUpdate.