65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 C# .NET 访问 EMC Isilon OneFS 7.2 API 的访问控制

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2015年2月2日

CPOL

5分钟阅读

viewsIcon

12885

介绍如何使用 C# .NET 的 Isilon OneFS REST API 来创建访问点、授予用户访问权限以及进行文件系统查询

引言

本文将深入探讨如何使用 C# .NET 的 Isilon OneFS 7.2 REST API。重点介绍如何通过 API 进行命名空间(文件/文件夹)的访问。本文将涵盖如何启用文件系统访问、创建访问点、创建用户和访问控制列表(ACLs),以及进行基本的文件系统查询。

背景

我当时正在为 EMC 的 Isilon 横向扩展 NAS 平台开发多个自动化和可视化应用程序,但找不到太多关于如何通过 .NET 访问 API 的信息。在我的第一篇文章《使用 C# .NET 访问 EMC Isilon OneFS REST API》中,我介绍了连接到 Isilon 集群、进行身份验证以及从集群查询数据的基本方法,以及如何将响应的 JSON 基本反序列化为 .NET 对象。请参考我之前的文章以开始。

文件系统访问

使用 OneFS API 访问 Isilon 集群上的文件和文件夹,类似于使用 SMB 或 NFS 协议访问文件和文件夹的方式。本质上,您需要一个访问点,这类似于 CIFS 共享或 NFS 挂载点。最初,只有 root 用户才能访问文件系统,因此您需要使用 root 帐户来创建用户、访问点并授予访问权限。

获取命名空间

使用 root 帐户 (root@clustername) 对集群进行身份验证后,您可以使用 /namespace/ 资源来获取 root 用户有权访问的所有访问点的列表。一开始,应该只有 /ifs/ 访问点。

首先,我们需要使用 root 凭据对集群进行身份验证(有关如何获取会话 cookie,请参阅上一篇文章)。然后,我们将对 /namespace/ 资源执行 HTTP GET 请求,以获取所有访问点的列表。

    ResourceString = "/namespace/";

    // Create a web request object pointing to the Isilon server and Resource String
    request = (HttpWebRequest)WebRequest.Create(IsilonServerAddress + ResourceString);

    // Set the cookie we received onto the request object
    request.Headers.Add("Cookie", cookie);

    response = (HttpWebResponse)request.GetResponse();

这是此 GET 查询返回的 JSON 响应。

{"namespaces":[{
   "name" : "ifs",
   "path" : "/ifs"
}
]}

以下是我用于将 JSON 响应反序列化为 .NET 对象的类。

    [DataContract]
    public class IsilonNamespaceResponse
    {
        [DataMember(Name = "namespaces")]
        public Namespace[] AccessPoints;
    }

    [DataContract]
    public class Namespace
    {
        [DataMember(Name = "name", IsRequired = true)]
        public string Name;

        [DataMember(Name = "path", IsRequired = true)]
        public string Path;
    }

最后,我们创建我们的 .NET 对象

    DataContractJsonSerializer js = 
           new DataContractJsonSerializer(typeof(IsilonNamespaceResponse));
    IsilonNamespaceResponse members = 
           (IsilonNamespaceResponse)js.ReadObject(response.GetResponseStream());

    foreach(Namespace accessPoint in members.AccessPoints)
    {
        // Do something against the access point
    }

创建访问点

在某些应用程序中,使用主访问点 /ifs/ 可能很有意义,但这将授予对整个 OneFS 文件系统的访问权限,因此强烈建议使用 API 进行文件系统访问的应用程序使用备用访问点。要创建访问点,您需要对 /namespace/ 资源执行 HTTP PUT 请求。请求的格式将是 /namespace/*NewAccessPointName*<name access="" create="" new="" of="" point="" to="">,请求的正文将是一个 JSON 对象,其中包含 OneFS 文件系统中访问点的绝对路径。

请求应如下所示

PUT /namespace/accesspoint1 HTTP/1.1 Host my_cluster:8080 
Date: Fri, 15 Mar 2013 21:51:50 GMT 
Content-Type: text/xml    
{      
    "path": "/ifs/home/"    
}

在这种情况下,我想创建一个名为“MyAccessPoint”的访问点,并将其指向 OneFS 文件系统上的一个名为 Share1 的文件夹。我已经在我 Isilon 集群上创建了此文件夹。

注意:在使用它创建访问点之前,文件系统上的绝对路径必须存在。否则,命令将返回 HTTP 400 消息。

为了创建访问点,首先我需要一个类来表示我们将发送到集群的 JSON 对象。

    [DataContract]
    public class AccessPoint
    {
        [DataMember(Name = "path")]
        public string AbsolutePath;
    }

接下来,我需要创建一个此类的实例,并为“path”属性指定文件系统中的绝对路径。同样,我需要在请求字符串中指定访问点名称。最后,我需要将该对象序列化到 HTTP PUT 请求对象的 RequestStream 中。

    string AccessPointName = "MyAccessPoint";

    AccessPoint MyAccessPoint = new AccessPoint();
    MyAccessPoint.AbsolutePath = "/ifs/Share1";

    ResourceString = "/namespace/" + AccessPointName;

    // Create a web request object pointing to the Isilon server and Resource String
    request = (HttpWebRequest)WebRequest.Create(IsilonServerAddress + ResourceString);
    request.Method = "PUT";
    request.ContentType = "text/xml"; // Not sure why xml, but that's what this request needs?

    // Set the cookie we received onto the request object
    request.Headers.Add("Cookie", cookie);

    // Write the AccessPoint object to the request stream
    DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(AccessPoint));
    js.WriteObject(request.GetRequestStream(), MyAccessPoint);
        
    // Should just be an 200 OK response... If not, something failed!
    response = (HttpWebResponse)request.GetResponse();

用户访问

现在我们有了访问点,我们将要管理该访问点的用户权限。(换句话说,不要在您的应用程序中使用 root 帐户进行生产文件系统访问!)首先,我们需要创建一个用户,遗憾的是,我没有看到通过 API 完成此操作的方法,但如果我找到了,我会修改这篇文章。但是,您可以使用 GUI 或 CLI 创建用户。例如,使用 CLI,请使用以下命令:

isi auth users create user1 --password user1 --home-directory /ifs/ home/user1 --password-expires no

在我们创建了用户之后,我们可以将这些用户的权限授予该访问点。我们需要传递的 JSON 对象如下:

{
   "authoritative":"acl", 
   "acl":
   [{
     "trustee": 
     {
        "name":"user1",
        "type":"user"
     }, 
     "accesstype":"allow", 
     "accessrights":["file_read"], 
     "op":"add"
   }]
}

首先,以下是创建此 JSON 对象所需的 .NET 类:

    // Trustee object (User or Group)
    [DataContract]
    public class Trustee
    {
        [DataMember(Name = "name")]
        public string UserName;

        [DataMember(Name = "type")]
        public string UserType;
    }

    // The ACL which specifies Trustee, type of access, rights, and the operation (add, remove)
    [DataContract]
    public class AccessControlList
    {
        [DataMember(Name = "trustee")]
        public Trustee Trustee;

        [DataMember(Name = "accesstype")]
        public string AccessType;

        [DataMember(Name = "accessrights")]
        public string[] AccessRights;

        [DataMember(Name = "op")]
        public string Operation;
    }

    // The request to create an ACL
    [DataContract]
    public class CreateACL
    {
        [DataMember(Name = "authoritative")]
        public string authoritative = "acl";

        [DataMember(Name = "acl")]
        public AccessControlList[] acl;
    }

现在,我们需要创建 ACL、受托人和请求对象以传递给 Isilon 集群。您需要创建一个 HTTP PUT 命令,并将 JSON 对象通过访问点传递,并带有以下查询参数 - aclnsaccess,它看起来像这样:“PUT /namespace/<access_point>?acl&nsaccess=true”。nsaccess 参数用于指示请求的操作是针对访问点本身,而不是存储路径。该值必须设置为 true,否则请求的行为将类似于针对访问点内数据的 getput 操作。

以下代码创建了我们将用于发送到 Isilon 集群的 .NET 对象。

     Trustee trustee = new Trustee();
     trustee.UserName = "user1"; // Specify the user we created above
     trustee.UserType = "user"; // user or group?

     AccessControlList acl = new AccessControlList();
     acl.Trustee = trustee;
     acl.Operation = "add";
     acl.AccessType = "allow";
     acl.AccessRights = new string[] { "file_read" };

     CreateACL createAcl = new CreateACL();
     createAcl.acl = new AccessControlList[] { acl };

现在,我们将对象传递给 Isilon 集群。

    string AccessPointName = "MyAccessPoint";

    ResourceString = "/namespace/" + AccessPointName + "?acl&nsaccess=true";

    // Create a web request object pointing to the Isilon server and Resource String
    request = (HttpWebRequest)WebRequest.Create(IsilonServerAddress + ResourceString);
    request.Method = "PUT";
    request.ContentType = "application/json";

    // Set the cookie we received onto the request object
    request.Headers.Add("Cookie", cookie);

    // Write the CreateACL object to the request stream
    DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(CreateACL));
    js.WriteObject(request.GetRequestStream(), createAcl);

    // Should just be an 200 OK response... If not, something failed!
    response = (HttpWebResponse)request.GetResponse();

假设此操作成功,然后我们可以对访问点执行 GET 请求,指定 acl & nsaccess=true 参数以获取我们访问点上所有 ACL 的列表。换句话说,GET /namespace/MyAccessPoint?acl&nsaccess=true,它将返回类似以下内容:

{
"acl" : 
[

{
"accessrights" : [ "file_read" ],
"accesstype" : "allow",
"inherit_flags" : [],
"trustee" : 
{
"id" : "UID:2000",
"name" : "user1",
"type" : "user"
}
}
],
"authoritative" : "acl",
"group" : 
{
"id" : "GID:0",
"name" : "wheel",
"type" : "group"
},
"mode" : "0040",
"owner" : 
{
"id" : "UID:0",
"name" : "root",
"type" : "user"
}
}

正如您所见,user1 现在已被授予对该访问点的只读访问权限。

目录操作

现在我们的用户可以访问访问点了,从 API 的角度来看,您可以做很多事情。基本操作是请求该访问点内所有对象(文件和文件夹)的列表。作为演示,我在我的访问点中填充了多个文档和文件夹。

要访问访问点内的信息,请从查询字符串中删除 acl & nsaccess=true 参数,然后将返回该访问点内所有对象的列表。

{"children":[{
   "name" : "Notepad.txt"
}
,{
   "name" : "MS Word Document.docx"
}
,{
   "name" : "MS Excel Spreadsheet.xlsx"
}
,{
   "name" : "MS Power Point Presentation.pptx"
}
,{
   "name" : "Folder1"
}
]}

结论

这是如何使用 Isilon OneFS API 通过 .NET 进行访问点、访问控制和文件系统访问的一个示例。希望您觉得这些信息很有用!

历史

  • 2015 年 2 月:初版
© . All rights reserved.