简体   繁体   English

使用Java控制Azure容器级访问策略

[英]Control Azure Container-Level Access Policy with Java

I am trying to design a Java application running on a local server that manages the access permissions to blobs that are being stored in Windows Azure. 我正在尝试设计一个在本地服务器上运行的Java应用程序,该应用程序管理对存储在Windows Azure中的Blob的访问权限。 The cloud storage resources are used by several mobile applications (also written in Java) that need read access, and sometimes temporary write access, to a single blob container hosted in the cloud. 多个移动应用程序(也用Java编写)使用云存储资源,这些应用程序需要对托管在云中的单个Blob容器进行读取访问,有时需要临时写入访问。 I am using the Windows Azure Plugin for Eclipse with Java by Microsoft Open Technologies . 我正在使用Microsoft Open Technologies的Java Eclipse Windows Azure插件

My question is: How (or if) container-level stored access policies in Azure can be made to revert to an earlier stored policy (READ) on expiration, rather than to “no access. 我的问题是:如何使Azure中的容器级存储访问策略 (或是否可以)在到期时恢复为较早的存储策略(READ),而不是“无访问权”。

The MSDN article: Creating a Shared Access Signature in Java provides a good start, but it doesn't yet say much about how to use container-level stored policies to manage shared access polies with Java . MSDN文章: 在Java中创建共享访问签名提供了一个很好的开始,但是对于如何使用容器级别的存储策略来管理Java共享访问权限,它还没有说太多。 I am learning Java and SAS, and because I was unable to find Java code examples similar to Access Control for Azure Blobs , I have included a short piece of Java code below to demonstrate my question. 我正在学习Java和SAS,并且由于找不到与Azure Blobs访问控制类似的Java代码示例,因此我在下面提供了一小段Java代码来演示我的问题。

The server application retrieves the private storage connection string used to connect to Azure storage. 服务器应用程序检索用于连接到Azure存储的私有存储连接字符串。 Gets the cloud storage account and creates a cloud blob client. 获取云存储帐户并创建一个云Blob客户端。 Gets a reference to the container and creates a container if it does not already exist. 获取对容器的引用,并创建一个尚不存在的容器。 (Note that a container name must be lower case.) It then downloads the current permissions for the container. (请注意,容器名称必须为小写。)然后,下载容器的当前权限。 (Up to five container-level stored access policies may be saved for a container.) (一个容器最多可以保存五个容器级别的存储访问策略。)

For example, let's say there are two stored access policies, named "baxter" and "heath," controlling the container-level permissions, and that both policies were set to READ (only) when the stored access polices of the container were previously saved. 例如,假设有两个存储的访问策略,分别名为“ baxter”和“ heath”,用于控制容器级别的权限,并且当先前保存了容器的存储访问策略时,这两个策略均设置为READ(仅) 。 These initial policies, with READ permissions, are set to expire after several months. 这些具有读取权限的初始策略被设置为在几个月后过期。 Mobile applications assigned to either the "heath" or "baxter" policies then start out with read-access to blobs stored in sascontainer6 through uri strings similar to: 然后,分配给“健康”或“ baxter”策略的移动应用程序首先通过uri字符串对sascontainer6中存储的blob进行读取访问,类似于:

http://grassy.blob.core.windows.net/sascontainer6/image4.jpg?sr=c&sv=2012-02-12&sig=5G7EOgiYYNEGmw2Y0T4IUgt%2FzTnmYpaxWfB5nEira08%3D&si=baxter

http://grassy.blob.core.windows.net/sascontainer6/image4.jpg?sr=c&sv=2012-02- 12&sig=llUoAg2PvFUfhO28ncrlheh2RRJdb7smQEX6nO8xoCk%3D&si=heath

As needed, the server application can elevate a subset of mobile applications to both READ and WRITE permissions without having to issue a new string. 根据需要,服务器应用程序可以将移动应用程序的子集提升为读取和写入权限,而不必发出新的字符串。 It can do so by modifying the "baxter" policy saved with the container. 它可以通过修改与容器一起保存的“ baxter”策略来实现。 The 'granularity' of the control is at the policy level, and the policy update enables all the mobile applications assigned to the "baxter” policy to write (or overwrite) blobs in the container. Mobile applications assigned to the "heath" policy continue to have READ (only) permission. In a similar way, the server application could revoke all access to the container for applications assigned to a particular policy. 控件的“粒度”处于策略级别,并且策略更新使分配给“ baxter”策略的所有移动应用程序都可以在容器中写入(或覆盖)blob。分配给“ heath”策略的移动应用程序将继续以类似的方式,服务器应用程序可以撤销分配给特定策略的应用程序对容器的所有访问。

Before changing the policy, the server application ensures that public access to the container has been turned OFF. 在更改策略之前,服务器应用程序确保已关闭对容器的公共访问。 It specifies the current time as the start time and an expiration time for access that is one hour after the start time. 它将当前时间指定为开始时间,并且将访问时间指定为开始时间之后的一小时。 It sets both the READ and WRITE permissions for the new policy. 它为新策略设置了READ和WRITE权限。 Finally, the existing "baxter" policy is overwritten with the new policy. 最后,现有的“ baxter”策略将被新策略覆盖。

The generateSharedAccessSignature method can get a shared access signature (SAS) for the "baxter" and "heath" policies. generateSharedAccessSignature方法可以获取“ baxter”和“ heath”策略的共享访问签名(SAS)。 Changing the permissions saved in the policy should not alter the SAS, and applications using the above uri strings should work until the specified expiration time. 更改策略中保存的权限不应更改SAS,使用上述uri字符串的应用程序应可以工作到指定的到期时间。

However, once the expiration time is reached, the “baxter” string will lose all permissions to the container, both READ and WRITE. 但是,一旦达到到期时间,“ baxter”字符串将失去对容器的所有权限,包括读取和写入。 But this is not what I want to happen. 但这不是我想要发生的事情。 I need the permissions for mobile applications assigned to the “baxter” policy to revert back to READ (only.) Because a SAS string with WRITE access enables anyone to write to the resource, the best practice is to keep the period between start time and expiration time as short as possible, no more than an hour. 我需要分配给“ baxter”策略的移动应用程序的权限才能还原为READ(仅)。由于具有WRITE访问权限的SAS字符串使任何人都可以写入资源,因此最佳做法是保持开始时间与启动时间之间的时间间隔。有效期越短越好,不超过一个小时。 Setting the permissions to READ for longer periods is acceptable for my particular application. 将较长的读取权限设置为我的特定应用程序是可以接受的。

My question is: How (or if) container-level shared access policies in Azure can be used to allow the token (ie the “baxter” string shown above) to revert back to an alternate policy (ie READ) rather than “no access.” 我的问题是:如何(或是否)使用Azure中的容器级共享访问策略来允许令牌(即上面显示的“ baxter”字符串)恢复为备用策略(即“ READ”)而不是“无权访问” 。”

public static void main(String[] args) throws InvalidKeyException, URISyntaxException, StorageException 
    {           
    Account creds = new Account();              
    final String storageConnectionString = creds.getstorageconnectionstring();
    CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
    CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
    CloudBlobContainer container = blobClient.getContainerReference("sascontainer6");
    container.createIfNotExist();
    BlobContainerPermissions containerPermissions = container.downloadPermissions();
    containerPermissions.setPublicAccess(BlobContainerPublicAccessType.OFF);
    SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy();
    GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    calendar.setTime(new Date());
    policy.setSharedAccessStartTime(calendar.getTime());
    calendar.add(Calendar.HOUR, 1);
    policy.setSharedAccessExpiryTime(calendar.getTime());
    policy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.WRITE));
    containerPermissions.getSharedAccessPolicies().put("baxter", policy);            
    container.uploadPermissions(containerPermissions);
    String sas = container.generateSharedAccessSignature(new SharedAccessBlobPolicy(),"baxter");         
    String sasex = container.generateSharedAccessSignature(new SharedAccessBlobPolicy(),"heath");         
    }

You demonstrate good understanding on the Container Level Access Policies along with SAS. 您对SAS一起对容器级别访问策略表现出了很好的理解。 All explained leads to the very specific question: 所有的解释导致一个非常具体的问题:

My question is: How (or if) container-level shared access policies in Azure can be used to allow the token (ie the “baxter” string shown above) to revert back to an alternate policy (ie READ) rather than “no access.” 我的问题是:如何(或是否)使用Azure中的容器级共享访问策略来允许令牌(即上面显示的“ baxter”字符串)恢复为备用策略(即“ READ”)而不是“无权访问” 。”

Which' answer, unfortunately is NO . 不幸的是,哪个答案是“ 否” You can't define a fallback for what happens when container level policy expires. 您无法定义容器级别策略到期后会发生的事件的fallback Once it expires, it's over, done. 一旦过期,就结束了。 No SAS associated with it can do any action over the resources associated. 与之关联的SAS不能对关联的资源执行任何操作。 You have to, from your server side code to explicitly overwrite it again with new permissions and new expiry date. 您必须从服务器端代码使用新的权限和新的有效日期再次显式覆盖它。

But I want to challenge your conceptual architecture a bit. 但是我想挑战一下您的概念架构。 You have a statement: 您有一个声明:

As needed, the server application can elevate a subset of mobile applications to both READ and WRITE permissions without having to issue a new string. 根据需要,服务器应用程序可以将移动应用程序的子集提升为读取和写入权限,而不必发出新的字符串。

Even if your server does so, how do the mobile clients understand that now they can also write , rather than only read from that cloud storage? 即使您的server这样做了,移动客户端如何理解他们现在也可以write而不是仅从该云存储中read I really doubt that your mobile clients always try to write silently die if they can't. 我真的怀疑您的移动客户端是否总是尝试默默地写死。

My guess is that there is some kind of server-to-mobile-client communication before the mobile client is aware that it can write to the cloud storage. 我的猜测是,在移动客户端意识到可以写入云存储之前,存在某种server-to-mobile-client通信。 If there is no such communication then something is terribly broken IMHO. 如果没有这样的通信,那么恕我直言,有些东西就坏了。

However, if such communication exists, then nothing will stop you from providing a new shorter living SAS with RW permissions (Read and Write). 但是,如果存在此类通信,则没有什么会阻止您提供具有RW权限(读取和写入)的新的更短寿命的SAS。 This new shorter living SAS can even be a stand alone SAS, meaning that it is not associated with any stored Access Policy, and is issued specifically for the reason of writing to the blobs. 这个新的较短的活动SAS甚至可以是stand alone SAS,这意味着它与任何存储的访问策略都不相关,并且是出于写入blob的原因而专门发行的。

My other guess is that you want to avoid sending SAS over the wire to avoid man-in-the-middle type attacks. 我的另一个猜测是,您希望避免通过电线发送SAS,以避免man-in-the-middle攻击。 And rather want to have the SAS pre-configured in the application. 而是希望在应用程序中预先配置SAS。 If this is the case, I can suggest to have separate Access Policies for Read and Write. 如果是这种情况,我建议您为读写设置单独的访问策略。 You can safely have an expired Access Policy for writing, and make it valid only when needed. 您可以放心使用expired的写入访问策略,并仅在需要时才有效。 Your device will have now 4 SAS URIs instead of 2, but it (the device) will know precisely which policy is Read Only, and using which can attempt writes. 您的设备现在将具有4个SAS URI,而不是2个,但它(该设备)将准确地知道哪个策略是“只读”,以及使用哪个策略可以尝试写入。 Note that even expired, the Write access policy is still associated with the container, it is not removed. 请注意,即使已过期,写访问策略仍与容器关联,但不会被删除。 So you just need to update it the next time you want it to be active. 因此,您只需要在下次激活它时对其进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM