Skip to content

Commit

Permalink
Merge pull request #486 from tumblr/will-write-encrypted-tags-permission
Browse files Browse the repository at this point in the history
Write encrypted tags permission
  • Loading branch information
william-richard authored Nov 10, 2016
2 parents 866e1f5 + 88dface commit ef74c4b
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 12 deletions.
2 changes: 2 additions & 0 deletions app/collins/controllers/Permissions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ object Permissions {

object Feature extends PermSpec("feature") {
def CanSeePasswords = spec("canSeePasswords", AdminSpec)
def CanSeeEncryptedTags = spec("canSeeEncryptedTags", AdminSpec)
def CanWriteEncryptedTags = spec("canWriteEncryptedTags", AdminSpec)
def NoRateLimit = spec("noRateLimit", AdminSpec)
}

Expand Down
2 changes: 1 addition & 1 deletion app/collins/controllers/actions/AssetAction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ trait AssetResultsAction {
protected def handleApiSuccess(p: Page[AssetView], details: Boolean): Result = {
val items = p.items.map {
case a: Asset => if (details){
a.getAllAttributes.exposeCredentials(user.canSeePasswords).toJsValue
a.getAllAttributes.exposeCredentials(user.canSeeEncryptedTags).toJsValue
} else {
a.toJsValue
}
Expand Down
3 changes: 1 addition & 2 deletions app/collins/controllers/actions/asset/GetAction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ case class GetAction(
}

protected def handleSuccess(asset: Asset): Result = {
val display = asset.getAllAttributes.exposeCredentials(user.canSeePasswords)
val display = asset.getAllAttributes.exposeCredentials(user.canSeeEncryptedTags)
isHtml match {
case true =>
Status.Ok(html.asset.show(display, user)(flash, request))
Expand All @@ -78,4 +78,3 @@ case class GetAction(
}

}

22 changes: 22 additions & 0 deletions app/collins/models/AssetLifecycle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import scala.util.control.Exception.allCatch

import play.api.Logger

import collins.controllers.Permissions

import collins.models.conversions.dateToTimestamp
import collins.models.logs.LogFormat
import collins.models.logs.LogSource
Expand Down Expand Up @@ -123,6 +125,13 @@ class AssetLifecycle(user: Option[User], tattler: Tattler) {
opts.find(kv => restricted(kv._1)).map(kv =>
return Left(new Exception("Attribute %s is restricted".format(kv._1))))
}

opts.find(kv => Feature.encryptedTags.contains(kv._1)).map(kv =>
if (user.isDefined && !user.get.canWriteEncryptedTags) {
return Left(new Exception("You do not have permission to write encrypted tags"))
}
)

Asset.inTransaction {
MetaWrapper.createMeta(asset, opts, groupId)
Asset.partialUpdate(asset, Some(new Date().asTimestamp), status, state)
Expand Down Expand Up @@ -190,6 +199,12 @@ class AssetLifecycle(user: Option[User], tattler: Tattler) {
filtered.find(kv => AssetLifecycleConfig.isRestricted(kv._1)).map(kv =>
return Left(new Exception("Attribute %s is restricted".format(kv._1))))

filtered.find(kv => Feature.encryptedTags.contains(kv._1)).map(kv =>
if (user.isDefined && !user.get.canWriteEncryptedTags) {
return Left(new Exception("You do not have permission to write encrypted tags"))
}
)

allCatch[Boolean].either {
val values = Seq(AssetMetaValue(asset, RackPosition, rackpos)) ++
PowerUnits.toMetaValues(units, asset, options)
Expand Down Expand Up @@ -221,6 +236,13 @@ class AssetLifecycle(user: Option[User], tattler: Tattler) {
val filtered = options.filter(kv => !requiredKeys(kv._1))
filtered.find(kv => AssetLifecycleConfig.isRestricted(kv._1)).map(kv =>
return Left(new Exception("Attribute %s is restricted".format(kv._1))))

filtered.find(kv => Feature.encryptedTags.contains(kv._1)).map(kv =>
if (user.isDefined && !user.get.canWriteEncryptedTags) {
return Left(new Exception("You do not have permission to write encrypted tags"))
}
)

val lshwParser = new LshwParser(lshw)
val lldpParser = new LldpParser(lldp)

Expand Down
25 changes: 24 additions & 1 deletion app/collins/models/User.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,30 @@ abstract class User(val username: String, val password: String) {
case false => None
}
def isEmpty(): Boolean = username.isEmpty && password.isEmpty && roles.isEmpty
def canSeePasswords(): Boolean = Permissions.please(this, Permissions.Feature.CanSeePasswords)
def canSeeEncryptedTags(): Boolean = {
// We want to rename canSeePasswords to canSeeEncryptedTags
// but want to keep backwards compatibility
// Try to use canSeePasswords if it is set, else fall back to canSeePasswords
//
// All permissions default to true, so we need to test if the permission is configured,
// and only use it if it is.
val canSeePasswords = AuthenticationProvider.permissions("feature.canSeePasswords") match {
case None => false
case _ => Permissions.please(this, Permissions.Feature.CanSeePasswords)
}
val canSeeEncryptedTags = AuthenticationProvider.permissions("feature.canSeeEncryptedTags") match {
case None => false
case _ => Permissions.please(this, Permissions.Feature.CanSeeEncryptedTags)
}
canSeeEncryptedTags || canSeePasswords
}
def canWriteEncryptedTags(): Boolean = {
AuthenticationProvider.permissions("feature.canWriteEncryptedTags") match {
case None => false
case _ => Permissions.please(this, Permissions.Feature.CanWriteEncryptedTags)
}
}

def isAdmin(): Boolean = hasRole("infra")
def toMap(): Map[String, String] = Map(
User.ID -> id().toString(),
Expand Down
10 changes: 5 additions & 5 deletions app/views/asset/show_overview.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ <h3>Asset Overview <small>System and user attributes</small></h3>
mv.getName match {
case "CANCEL_TICKET" => optionDisplay(0, SoftLayerHelper.ticketLink(mv.getValue), mv.getValue)
case "DISK_STORAGE_TOTAL" => optionDisplay(1, Some(mv.getValue), mv.getValue)
case encrypted if Feature.encryptedTags.contains(encrypted) => if(user.canSeePasswords) { TagDecorator.decorate(mv) } else { "********" }
case encrypted if Feature.encryptedTags.contains(encrypted) => if(user.canSeeEncryptedTags) { TagDecorator.decorate(mv) } else { "********" }
case _ => TagDecorator.decorate(mv)
}
}
Expand Down Expand Up @@ -196,7 +196,7 @@ <h3>Hardware Summary <small>Summary of system components reported by LSHW</small
<td>Hyperthreading Enabled</td>
<td>@{if (aa.lshw.hasHyperthreadingEnabled) "Yes" else "No"}</td>
</tr>

<tr>
<th colspan="3">Memory</th>
</tr>
Expand All @@ -220,7 +220,7 @@ <h3>Hardware Summary <small>Summary of system components reported by LSHW</small
<td>Unused Memory Banks</td>
<td>@aa.lshw.memoryBanksUnused</td>
</tr>

<tr>
<th colspan="3">Disks</th>
</tr>
Expand All @@ -244,7 +244,7 @@ <h3>Hardware Summary <small>Summary of system components reported by LSHW</small
<td>Has PCIe Flash Disk</td>
<td>@{if (aa.lshw.hasFlashStorage) "Yes" else "No"}</td>
</tr>

<tr>
<th colspan="3">Network</th>
</tr>
Expand All @@ -258,7 +258,7 @@ <h3>Hardware Summary <small>Summary of system components reported by LSHW</small
<td>Has 10Gb Interface</td>
<td>@{if (aa.lshw.has10GbNic) "Yes" else "No"}</td>
</tr>

<tr>
<th colspan="3">Power</th>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion conf/authentication.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
authentication {

type = default
file.userfile = "conf/users.conf"
permissionsFile = "conf/permissions.yaml"

}
1 change: 1 addition & 0 deletions conf/dev_base.conf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ features {
useWhitelistOnRepurpose = true
keepSomeMetaOnRepurpose = [ attr1, attr2 ]
deleteSomeMetaOnRepurpose = [ attr4, attr3 ]
syslogAsset = tumblrtag1
# default values below
#searchResultColumns = [ TAG, HOSTNAME, PRIMARY_ROLE, STATUS, CREATED, UPDATED ]
}
Expand Down
2 changes: 1 addition & 1 deletion conf/docker/permissions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ users:
permissions:
feature.noRateLimit:
- "u=tools"
feature.canSeePasswords:
feature.canSeeEncryptedTags:
- "u=admins"
- "u=tools"
- "g=infra"
Expand Down
4 changes: 3 additions & 1 deletion conf/permissions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ users:
permissions:
feature.noRateLimit:
- "u=tools"
feature.canSeePasswords:
feature.canSeeEncryptedTags:
- "u=admins"
- "u=tools"
- "g=infra"
- "g=ops"
- "g=sre"
feature.canWriteEncryptedTags:
- "u=admins"
controllers.Admin:
- "g=infra"
- "g=ops"
Expand Down

0 comments on commit ef74c4b

Please sign in to comment.